File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error in class level variable

 
Hemant Agarwal
Ranch Hand
Posts: 138
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 229
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try to declare and initialise variable a in try-finally scope
 
Hemant Agarwal
Ranch Hand
Posts: 138
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Tony Morris
Ranch Hand
Posts: 1608
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Hemant Agarwal
Ranch Hand
Posts: 138
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vlado Zajac
Ranch Hand
Posts: 245
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 138
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why error is not coming in this case.
a is still not initialized
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic