------------------------- local variable tmp is not initialized by an unconditional assignment but it still compiles. this contradicts what mughal says in his java book. Can someone help? Thanks, Akhilesh [ Jess added UBB [code] tags to preserve whitespace ] [ June 09, 2002: Message edited by: Jessica Sant ]
that's because the compiler knows because of the straight if/else statement that the 'tmp' variable is guaranteed to be initialized. If you adjust your if-else statement to the following, it will not compile, complaining that the variable might not have been initialized:
Hi Akhilesh, The code compiles because temp variable is initialized in the if condition as well as the else condition.If the if condition is executed,temp will be initialized,and if the if condition is not executed,then also temp will be initialized.Thats why,there is no compile time error. For more information,you can read the JLS section of unreachable statements and definite assignment.Those are very informative chapters and will certainly improve your knowledge.
thanks all, if there had been another 'if' instead of 'else' then the code won't compile because the compiler is not sure that the condition will be satisfied and the var will ever be initialized.
Thanks,<br />Akhi
Everyone is a villain in someone else's story. Especially this devious tiny ad: