File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Variable might not have been initialized Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Variable might not have been initialized" Watch "Variable might not have been initialized" New topic
Author

Variable might not have been initialized

John Johnson
Ranch Hand

Joined: Nov 05, 2006
Posts: 39
Please explain why this code doesn't compile:
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Variable x might not have been initialized

The try catch might fail, and a parent of Exception might be thrown, such as Throwable. As such, the variable of x may not get initialized.

Assume the try block fails, and assume the catch block is never entered - then the variable will never be initialized.

try this:




-Cameron McKenzie
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
There are some other interesting examples of if-else and switch also

and


~Sanjeev Singh<br />SCJP 1.5
Anthony Karta
Ranch Hand

Joined: Aug 09, 2004
Posts: 342
why below does not compile as well?? I thought I have cover all integer possibilities here.



SCJP 5
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
Both of the if checks which will be done at the runtime.To make it a compile time check make n a compile time constant by declaring it final.Now the following code will compile fine.
 
jQuery in Action, 2nd edition
 
subject: Variable might not have been initialized