Community
Participate
Working Groups
Consider that code: String a,b; do{ a="Hello "; }while(a!=null); if(a!=null) { b="World!"; } System.out.println(a+b); obviously a!=null and the first loop terminates. Because conditional statement in if-clause is true, b and c are set to "Hello " and "World!" respectively. Parser recognizes, that variable b may not have been initialized. Of course this code is rubbish and noone will ever write something like that, but shouldn't parser allow a programmer to compile a correct piece of code?
the if condition is not a constant, therefore it is possible that the then clause is not executed. So b might not be initialized. This is perfectly fine. You can notice that javac and jikes are reporting the same problem. Ok to close?
Closing. The compiler is implementing the definite assignment rules strictly. Rather blame the language specification.
I suppose you're right. Eclipse works in the same way as the compiler, so we cannot say, that this is as a bug. Although I still think, that it's silly.
Conservative flow analysis is quite restrictive indeed. Also note that the null check 'if (a!=null)' is useless, and removing it will make your problem go away. FYI, we are working in adding an optional warning to our compiler to diagnose such unnecessary null checks.
You'll get something like: ---------- 1. WARNING in d:\src\X.java (at line 9) if(a!=null) ^ The variable a cannot be null; it was either set to a non-null value or assumed to be non-null when last used ----------