jQuery in Action, 2nd edition*
The moose likes Java in General and the fly likes Method inner class & Final local variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Method inner class & Final local variables" Watch "Method inner class & Final local variables" New topic
Author

Method inner class & Final local variables

Alpesh Parekh
Greenhorn

Joined: Aug 05, 2002
Posts: 12
Because the local variables aren’t guaranteed to be alive as long as the method-local inner class object, the inner class object can’t use them. Unless the local variables are marked final!

But how come local varibles which are final are allowed to access?


<b>One life, live it.</b><br /><b>Alpesh</b>
Mehul Sanghvi
Ranch Hand

Joined: Feb 04, 2002
Posts: 134
Hi,

I am not sure about this but I think the reason is:

Local variables are stored on the stack and as soon as the method call finishes the stack is popped up and local variables are inaccessible to the object of local inner class.

Whereasa FINAL local variables are stored in the data section of the memory, potentially allowing JVM to access them even after the end of method call.

Regards,
Mehul K. Sanghvi.
Zoots Allures
Greenhorn

Joined: Oct 19, 2004
Posts: 23
As you say, we cannot access local variables from within a local inner class, but here is a trick you can use:


Now the trickInt array reference is final (1), but you can change the value of it's contents (2).

I think the reason why local variables (from the primitive type) have to be final, has to do with object lifetime. From within the method in which the local class is created, you could pass its (object) reference outside. Then you would run into problems when the inner class tries to access variables that do not exist after the method has finished.
[ October 19, 2004: Message edited by: Zoots Allures ]

Only in IT for the money!<br />Zoots.
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
The final keyword behaves differently when applied to primitives and object references. With a primitive, final makes the value a constant, however with an object reference, final makes the reference a constant. This also applies to arrays. So once the reference is initialised, it cant be changed to point to another object or array, however the object or array can be modified.
[ October 19, 2004: Message edited by: Nigel Browne ]
Alpesh Parekh
Greenhorn

Joined: Aug 05, 2002
Posts: 12
Guys, I undoorsdtood the concept of fonal object ref.
But my point is how JVM treats final local variable? It must be different then the non-final local variables.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Alpesh Parekh:
Because the local variables aren’t guaranteed to be alive as long as the method-local inner class object, the inner class object can’t use them. Unless the local variables are marked final!

But how come local varibles which are final are allowed to access?


What happens is that the compiler creates a field in the inner class and copies the value of the local variable to it. This is save because the value of the variable can't change. If the variable were not final, you could get quite confusing results - changing the value of the local variable not being reflected inside the inner class, that is.

Does that help?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608

With a primitive, final makes the value a constant, however with an object reference, final makes the reference a constant


This is a very common myth that causes confusion in different contexts.
One that immediately springs to mind is the Sun Coding Convention for constants (which applies to constants, not finals).

To draw an analogy between a constant and a final is like drawing an analogy between a wheel and a car. They are not the same thing, but they are related.

A constant is very well defined in JLS 15.28.
A constant is not the same thing as a final.


Tony Morris
Java Q&A (FAQ, Trivia)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method inner class & Final local variables