Meaningless Drivel is fun!*
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
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

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Variable might not have been initialized
 
Similar Threads
white spaces and CPU time
x-- -x/0??
Inserting a new item in arryalist.
jiris question about shift operator+addition
Operator