• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Final variable access from class inside function

 
Naresh Chaurasia
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • 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.
 
Henry Wong
author
Marshal
Pie
Posts: 21446
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • 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.
 
Michael Angstadt
Ranch Hand
Posts: 277
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • 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.
 
Lalit Mehra
Ranch Hand
Posts: 384
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Henry ... nice
 
Jean-Christian Imbeault
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • 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?

 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • 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.
 
Piotr Nowicki
Ranch Hand
Posts: 611
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • 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 :-)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic