This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Actually the example you showed should compile just fine (except for the syntax errors). Look at this: This example compiles without error as shown, however if you uncomment the last line you will get a compile time error: What's happening is that you can't use a reference until its been definitely assigned, the compiler cannot guarantee that the while loop will ever execute, therefore it cannot guarantee that 'var' will be definitely assigned. [ June 30, 2007: Message edited by: Garrett Rowe ]
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Joined: Jan 17, 2006
Just to throw another log on this fire, this compiles without error: because the compiler can infer that the loop will definitely execute at least once. [ June 30, 2007: Message edited by: Garrett Rowe ]
I may not know the right way to say it, but, I have noticed in several texts that, even though in the order of your method's sequence you ARE setting the variable before it is used, IF it is down in nested logic, it can STILL throw the compiler and cause a warning to be generated. Those books say that the correct thing to do in these cases is to initialize the variable to some reasonable value when it is declared.
SCJP - 86% - June 11, 2009