File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Local variable initialization error" Watch "Local variable initialization error" New topic

Local variable initialization error

ujjawal rohra
Ranch Hand

Joined: Mar 20, 2010
Posts: 105

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

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:
subject: Local variable initialization error
jQuery in Action, 3rd edition