0

Suppose we have a case where a variable is declared in certain conditions:

if (x = 1)
 boolean z = true;

Later on, we'd like to test if the variable z exists with try-catch. Is this possible and if so, what exception should we catch?

try {
 if (z)
  //do smth
} catch (<exception?> ex) {
 //do smth_else
}

Of course it would be possible to declare z before and change it's value accordingly in if block, but just hypothetically, is the above possible in Java? Python for example has NameError that will be raised when accessed local or global variable is not declared.

Thanks!

HSI.

1
  • 2
    Its not possible in java Commented Dec 4, 2012 at 10:13

7 Answers 7

2

What if you declared your variable like this:

Boolean x = null;

In that case you could check for it being null or not.

An even better alternative would be using an enum to represent the uninitialized value:

public enum MyEnum {
    UNINITIALIZED, TRUE, FALSE;
}

because if you will try to maintain your code several months later you (or someone else) may be puzzled about the Boolean being null.

Sign up to request clarification or add additional context in comments.

3 Comments

Boolean itself has Boolean.TRUE object why not use it?
Check my last sentence and this question.
Yep, it took some time till I got used to it. :)
2

we'll get compilation error if the variable we are using is not declared or is not visible in current scope. If it is declared we can check for NullPointerException if that was object. In case of primitive data types we should check for default values.

Comments

1

Suppose we have a case where a variable is declared in certain conditions:

Well it is difficult to assume because that would not compile:

  • you should use == to test for equality
  • you can't declare a variable in an if statement unless there is a block

Now assuming you enclose that declaration inside a block, the scope of that variable would be that block and you wouldn't be able to use it in your try / catch block (unless it is inside the if block of course, but I don't think that's what you want).

1 Comment

That explains the case. Variable simply does not exist outside if block and so try-catch would not make sense.
0

No, this is not possible in Java. You have to have the variable declared before you can refer to it, otherwise you will get a compilation error.

Comments

0
Boolean z = null;
if (x = 1){
  z = true;
}


if(z == null){
 //not initialized

}else{
  //initialized
}

Comments

0

it's not possible, Java is a strongly typed programming language because every variable must be declared with a data type before it can be used.

int x = 1;
boolean z = null;

if (x == 1)
 z = true;

try {
 if (z)
  //do smth
} catch (NullPointerException npe ) {
 //do smth_else
}

Comments

0

So far I understand , you wont be able to compile this piece of code. I can not remember any exception class but what I think is even if you "Invent" exception for this type of error. It won't compile. Because default values of primitive types are assigned to uninitialized class variables called fields but for variable used in method body, it gives compile time error

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.