Consider the following two similar program code . class constant{ public static void main(String str[]){ int i ; final boolean b = true; if (b) //b is final so if loop shud execute alwayz! i = 10; System.out.println("i = " + i); //gives variable not initialized error } }
and class constant{ public static void main(String str[]){ int i ; if (true) //compile time constant i = 10; System.out.println("i = " + i); //compiles fine . } }
In the first block since boolean b is final it shud be treated as a compile time constant and hence during compilation itself if(b) shud be replaced by if(true) and so shud not give any "variable not initialized error " Please help Thanx a lot arvind
Arvind I have tried your program it does work ok, both the first and second, and I don't get any errors at all. make sure you are not missing out anything somewhere. I used Jbuilder to compile and run the exact same programs. and in both of them , they compile and run perfectly with no errors.
A Software life cycle can be greater than its Developer's
Arvind, It seems that you have found a bug in JDK1.2. I have tried your example with JDK1.1, JDK1.2, and JDK1.3. It works in both 1.1 and 1.3, but the error happens with 1.2! Interesting, Manfred.
hi arvind both the code compile fine with me.i have SDK 1.3
Originally posted by Jane Griscti: Arvind, Looks as if Manfred is right ... you found a 'bug'. There is something very similar reported in the Sun Bug database compile-time constants not used correctly for 'unreachable statements' It reportedly does not occur in JDK 1.3 Hope that helps
Cherry: It did not work for while loops in my hands. I use jdk1.2 The question to all of you: if such a question occurs during the exam, what should we do?
Originally posted by Cherry Mathew: but finals work in while loop try all combinations try giving final boolean b = true ; while(b){ } system.out.println("cherry"); Cherry
Hi, if (b) i = 10; System.out.println("i = " + i); In this part of code posted by Arvind there is no brackets for if so compiler takes a precuation assuming that if case is of false then in that case varible i will not be initialize and as it is a local variable so compiler is giving : Variable i may not have been initialized. System.out.println("Value of i is " + i); This error is related to variable i not because of b. Code posted by Jane whereas have brackets in if body and System.out.println statement is inside that so it compiles sucessfully. If I understood code incorrectly then please correct me.
Hi Ishaan, If <code>b</code> wasn't a <code>final</code> variable, you would be right; the braces make a big difference, however, in this instance they don't matter.
The above code compiles without error because <code>b</code> is <code>final</code> and will equal <code>true</code> for the duration. If you remove the <code>final</code> keyword:
You get a compile error: variable i might not have been initialized. Hope that helps.
------------------ Jane Griscti Sun Certified Java 2 Programmer "When ideas fail, words come in very handy" -- Goethe