• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Final variable access from class inside function

 
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
If i try to compile the following code:


It works fine. But if i change final int a to int a. Compilation fails. What is the reason that the code works for final local variable.
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Short answer. It is defined in the specification as such.

Longer answer. Technically neither should work. The scope of the class (even method local class), is not the same as the scope for local variables. It is possible to store objects (or return objects) of the method local class, and hence, it is possible for the local variable to be out of scope when the object needs them.

Now... in the specification, final variables are allowed. The reason is, since final variables do not change, and it is possible for the compiler to detect whether it has been initialized, that if so... it is allowed, because the inner class can make a copy of the variable.

Henry
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more thing, The final local variable of a method becomes an instance field of the method local inner class.
 
Ranch Hand
Posts: 282
Eclipse IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Inner classes in Scala make so much more sense. I don't know if you have to make variables final like this, but private members are actually private in Scala. So you can't access an inner class' private members from the outer class like you can in Java.
 
Ranch Hand
Posts: 384
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ Henry ... nice
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
The reason is, since final variables do not change, and it is possible for the compiler to detect whether it has been initialized, that if so... it is allowed, because the inner class can make a copy of the variable.



Henry I like your answer in that both should be disallowed, final or not.

But I don't understand your second part. Even if the variable was not declared final the inner class could still make a copy, no?

 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jean-Christian Imbeault wrote:But I don't understand your second part. Even if the variable was not declared final the inner class could still make a copy, no?


If it wasn't final the inner class could make a copy, but then the original value could be changed. This could lead to confusing behaviour in some cases - it's probably safest that it's not possible.
 
Ranch Hand
Posts: 623
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys for this explanation. Since now I've thought that the final modifier somehow changes the place where the variable lives.

It's because first I've read (K. Sierra & B. Bates) that all local variables lives on the stack. That's why the method inner class cannot use these variables, as they might not exist anymore (the method could finished) while the object is still alive.
Then I've read that it's possible to access final local variables / parameters and asked myself: why? Does the final modifier changes the place in which the variable resides - from stack to heap or what?

Thanks for clearing this up - maybe someone in the future who will be preparing for SCJP will have this cleared up :-)
reply
    Bookmark Topic Watch Topic
  • New Topic