wood burning stoves*
The moose likes Beginning Java and the fly likes Error in class level variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Error in class level variable" Watch "Error in class level variable" New topic
Author

Error in class level variable

Hemant Agarwal
Ranch Hand

Joined: Nov 21, 2005
Posts: 138


I know that class level variables are initialized to default values
int to 0, boolean to false etc.
So there is no error in this case



Error : The local variable a may not have been initialized
Now why this error?
Why is this error coming?
Sanjaya Sugiarto
Ranch Hand

Joined: Mar 25, 2004
Posts: 229
try to declare and initialise variable a in try-finally scope


<a href="http://www.wi.hs-furtwangen.de" target="_blank" rel="nofollow">Business Information Technology - Hochschule Furtwangen University, Germany</a>
Hemant Agarwal
Ranch Hand

Joined: Nov 21, 2005
Posts: 138
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
It is because a fundamental of Java is that any piece of code may throw an exception (go figure?). An assignment might, in which case, a is not definitely assigned. JLS Chapter 16 is your point of reference.


Tony Morris
Java Q&A (FAQ, Trivia)
Hemant Agarwal
Ranch Hand

Joined: Nov 21, 2005
Posts: 138
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Hemant, why are you using the [code] tag for text that's not code at all? It looks very odd.

In the code show, if an exception occurs in the finally block, it will be thrown from the method before it reaches the return statement. So it doesn't matter if the variable a has not been assigned, because we never use a for anything. In contrast, in the second code sample from your first post, the "catch (Exception e)" means that if an exception were somehow thrown from the line a = 2, it would be caught, and then execution would proceed to the "return a". This would be problematic because in this case, a has not been assigned.

[Tony]: It is because a fundamental of Java is that any piece of code may throw an exception (go figure?).

Or at least, for purposes of determining whether a given variable is definitely assigned, the compiler is required to assume that any piece of code might throw a RuntimeException or an Error. That's not quite the same as saying it might actually happen. I'm guessing the rules were written this way because otherwise there would be a slippery slope for compiler authors - how much analysis should be performed to determine whether a given piece of code is reachable, or whether a given variable has been definitely assigned or not? Behavior among compilers would vary significantly depending on how smart they were, and code that was poorly written but functional might pass on one compiler and not on another. Not that there aren't variances between compilers anyway, but I think they'd be much greater if the JLS didn't require certain simplifying assumptions in this particular area.


"I'm not back." - Bill Harding, Twister
Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 245
Variable a is not class level variable, it is a local variable (it is defined inside a method). Local variables are not initialized to default values.
Hemant Agarwal
Ranch Hand

Joined: Nov 21, 2005
Posts: 138


Why error is not coming in this case.
a is still not initialized
 
 
subject: Error in class level variable
 
Similar Threads
try catch finally blocks
initializers-referencing
Please explain final
Implicit cast
final variable initialization