Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java check to see if a variable has been initialized

I need to use something similar to php's isset function. I know php and java are EXTREMELY different but php is my only basis of previous knowledge on something similar to programming. Is there some kind of method that would return a boolean value for whether or not an instance variable had been initialized or not. For example...

if(box.isset()) {   box.removeFromCanvas(); } 

So far I've had this problem where I am getting a run-time error when my program is trying to hide or remove an object that hasn't been constructed yet.

like image 217
CaldwellYSR Avatar asked Sep 22 '11 04:09

CaldwellYSR


People also ask

How do you check if a variable has been initialized?

Use the typeof operator to check if a variable is defined or initialized, e.g. if (typeof a !== 'undefined') {} . If the the typeof operator doesn't return a string of "undefined" , then the variable is defined.

How do you check if an int is initialized?

For a field variable you can check by comparing the int to 0 that is the default value for an int field : private int x: // default 0 for an int ... public void foo(){ if (x == 0){ // valid check // ... } }

Are variables initialized in Java?

Initialization of a static variable is not mandatory. Its default value is 0. If we access a static variable like an instance variable (through an object), the compiler will show a warning message, which won't halt the program. The compiler will replace the object name with the class name automatically.

How can an object be tested to find out if it is initialized?

Just check simply with a if-clause if object ain't null. If that's the case it has been initialized.


1 Answers

Assuming you're interested in whether the variable has been explicitly assigned a value or not, the answer is "not really". There's absolutely no difference between a field (instance variable or class variable) which hasn't been explicitly assigned at all yet, and one which has been assigned its default value - 0, false, null etc.

Now if you know that once assigned, the value will never reassigned a value of null, you can use:

if (box != null) {     box.removeFromCanvas(); } 

(and that also avoids a possible NullPointerException) but you need to be aware that "a field with a value of null" isn't the same as "a field which hasn't been explicitly assigned a value". Null is a perfectly valid variable value (for non-primitive variables, of course). Indeed, you may even want to change the above code to:

if (box != null) {     box.removeFromCanvas();     // Forget about the box - we don't want to try to remove it again     box = null; } 

The difference is also visible for local variables, which can't be read before they've been "definitely assigned" - but one of the values which they can be definitely assigned is null (for reference type variables):

// Won't compile String x; System.out.println(x);  // Will compile, prints null String y = null; System.out.println(y); 
like image 139
Jon Skeet Avatar answered Sep 24 '22 01:09

Jon Skeet