null
for all object types, false
for boolean primitive and 0 for numeric primitives. But local variables inside a method have no defaults.Consider the following code snippet:
public static void main(String[] args){It will fail to compile with this error:
java.util.Date d;
System.out.println(d);
}
variable d might not have been initializedHowever, the following will compile and run successfully:
System.out.println(d);
1 error
public static void main(String[] args){Because the rule is that all local variables must be initialized before they are first read. So it's perfectly OK to first declare a local variable without initializing it, initialize it later, and then use it:
java.util.Date d;
}
public static void main(String[] args){It may be an old habit from languages like C, where you need to declare all local variables at the beginning of a code block. But in Java with this rule in place, it's best to defer declaring local variables till needed.
java.util.Date d;
//do more work
d = new java.util.Date();
System.out.println(d);
}
Tags:
well this rule is most frustrating when trying to close a resource in a finally block. if I instantiate the resource inside a try, but try to close it within the finally, I get this error. if I move the instantiation outside the try, I get another error stating that a it must be within a try.
ReplyDeletevery frustrating.
Thanks for posting this! I searched on google and this page was the first result, very good to know!
ReplyDeleteIf I am declaring PI, how about that,
ReplyDeleteDo I have to specifically mention the value of PI(If so then wt the hell is this error) which I am getting for PI
Sometimes when you use for, while, etc. to initialize you need to use for example:
ReplyDeletevariable1 = null;
That message will dissapear.
Thanks! About 1 hour trying to find out... and in one minute you saved me.
ReplyDeleteThank you, that was very helpful! :D
ReplyDeletemaster
ReplyDeleteThis is actually a huge feature of Java, and you should absolutely get out of the habit of typing "=0" or "=null" when declaring variables.
ReplyDeleteThis uses branch code checking to ensure that every possible path that could get to the use of a given variable has, at some point, initialized that variable.
If you go around setting them to 0, you will eliminate this extremely useful warning. If you are getting the warning, look at it closely to figure out if you are missing something. Sometimes you still have to initialize it, but now I never do until I'm sure that's the best way to code it.
I wrote the following code to set a variable to an unused, random value:
ReplyDelete// Find an empty place in tmp
boolean foundEmptyPlace = false;
while (!foundEmptyPlace) {
randomNumber = randomGenerator.nextInt(tmp.length);
if (tmp[randomNumber] == null) {
foundEmptyPlace = true;
}
}
// do something with tmp[randomNumber]
Here I get the "might not have been initialized" error, though reasonably it must have been initialized within the while loop. My solution was to initialize randomNumber (= 0) before the loop. But, as Bill points out, this seems to be an unpreferrable habit. Can I avoid the error in a more clean and correct way?