Take a look at below code: As you see while trying to access static field x via an uninitialized local variable Foo foo; code foo.x generates compilation error: Variable ‘foo’ might not have been initialized. It could seem like this error makes sense, but only until we realize that to access a static member the JVM doesn’t actually use the
Tag: initialization
Variable might be assigned in loop?
I don’t know why the code is not compiling when final variable is initialized in loop and loop iterate only one time? Is Loop is somehow running more than one time and multiple assignments is done to variable x? Answer The compiler does not care how many times the code in the loop will be executed at run time. To
Necessity of static block in Java
I found that in Java, there is a feature called static block, which includes code that is executed when a class is first loaded (I don’t understand what ‘loaded’ means, does it mean initialized?). Is …
Java: “Local variable may not have been initialized” not intelligent enough?
Consider the following method: void a () { int x; boolean b = false; if (Math.random() < 0.5) { x = 0; b = true; } if (b) x++; } On x++ I get the "…