• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Error in class level variable

 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try to declare and initialise variable a in try-finally scope
 
Hemant Agarwal
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Why error is not coming in this case.
a is still not initialized
reply
    Bookmark Topic Watch Topic
  • New Topic