Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method inner class & Final local variables

 
Alpesh Parekh
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Mehul Sanghvi
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Nigel Browne
Ranch Hand
Posts: 703
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic