Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Final variable access from class inside function 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Final variable access from class inside function" Watch "Final variable access from class inside function" New topic
Author

Final variable access from class inside function

Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 356
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.

SCJP 1.4, SCWCD1.4, OCA(1Z0-007)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18546
    
  40


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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

One more thing, The final local variable of a method becomes an instance field of the method local inner class.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 273

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.


SCJP 6 || SCWCD 5
Lalit Mehra
Ranch Hand

Joined: Jun 08, 2010
Posts: 384

@ Henry ... nice


http://plainoldjavaobject.blogspot.in
Jean-Christian Imbeault
Greenhorn

Joined: Dec 08, 2004
Posts: 19
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

Joined: Apr 06, 2010
Posts: 4344
    
    8

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

Joined: Jul 13, 2010
Posts: 610

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 :-)


OCP Java SE 6 Programmer, OCM Java SE 6 Developer, OCE Java EE 6 JSPSD, OCE Java EE 6 EJBD, OCE Java EE 6 JPAD, Spring 3.0 Core Professional.
 
Don't get me started about those stupid light bulbs.
 
subject: Final variable access from class inside function
 
Similar Threads
where final variable reside ?
Doubts regarding Method Local Inner Class
Doubts regarding Method local inner class
final loacl variables
Inner classes