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 Local variable initialization error 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 "Local variable initialization error" Watch "Local variable initialization error" New topic
Author

Local variable initialization error

ujjawal rohra
Ranch Hand

Joined: Mar 20, 2010
Posts: 103


Why is the compiler complaining at lines 2 and 3 that x is not initilized when it gets tha value 42 at line 1 ? ?


SCJP 6
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
because it refers to it's declaration 'int x'. Who ensures that the try block will ever run to completion and assign a value to x ?

So, initialize the int upon declaration.


JDBCSupport - An easy to use, light-weight JDBC framework -
Harpreet Singh janda
Ranch Hand

Joined: Jan 14, 2010
Posts: 317

Try -catch signify that there could be an exception, means we are not sure whether the particular line of code in try-catch will execute or not. So for compiler it is not sure that whether the line which is initializing the variable x will run or not and hence throws an exception.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Local variable initialization error
 
Similar Threads
Question
Exception
accessing the super class variable
error in result
extends class in another pkg