In general, it is very similar to the compiler feature:
public static void testvar() { int a; boolean c = false; // if (c) a = 0; else a = 1; for (int i=0; i<10; i++) { if (c) a = 0; else a = 1; // a = 1; } System.out.println("a is " + a); }
It does not compile ( Error:(23, 38) java: variable a might not have been initialized ), even if you leave a = 1; instead of if in the loop a = 1; . And if you uncomment the line before the loop, it is compiled.
S-shny compiler, for example (for me) does not give vorning on
int a, f=0; if (f) a = 1; a++;
but if you remove if (or change to if(0) ), it already gives)
P.S. The proposed answer naturally solves the problem (and much better in terms of style)
...? - Grundy