Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method-local inner class & final variables

 
Yogesh Mashalkar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

in the following eg :


the inner class cannot reference the local variable unless its final..
as the variable will die on the stack after method scope is over..

but i did not understand how it is able to access even the 'final' variable..as even the 'final' variable should die on the stack after method scope..the final 'just' doesnt allow reinitialization of the variable.

maybe i am unclear of the 'final' modifier applied to local variable here..

if anyone can help?
thnx
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the variable is final, its value can be copied into the instance as a constant. This way, the variable doesn't need to be read after the object is created, so the variable can be destroyed when the method exits.
 
Yogesh Mashalkar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that makes things clear!

thanks for the response
 
yogi maheshnath
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by marc weber:
Because the variable is final, its value can be copied into the instance as a constant. This way, the variable doesn't need to be read after the object is created, so the variable can be destroyed when the method exits.



Please Explain more.I am not getting it.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by yogi maheshnath:
... Please Explain more.I am not getting it.

An instance of a local class can continue to exist after the method exits. But when a method exits, local variables are destroyed. So if the instance tried to read one of those local variables after the method has exited, there would be a problem. However, if the local variable is final, then its value will not change, and so that value can be copied into the instance as a constant. That way, the local variable does not need to be read again after the instance is created.
 
Sandeep Bhandari
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by marc weber:

However, if the local variable is final, then its value will not change, and so that value can be copied into the instance as a constant. That way, the local variable does not need to be read again after the instance is created.


Are you talking about the instance of member local class??
Who will copy the value of final variable to instance as we are just reading its value??
 
Matthew Walton
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Java runtime takes care of the copying, I believe at the time the method of the inner class is invoked.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sandeep Bhandari:
... Are you talking about the instance of member local class??
Who will copy the value of final variable to instance as we are just reading its value??

Yes, the "instance" I'm referring to is an instance of the local class. The value of the final variable is copied behind the scenes.
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instance variable is defined at classlevel.
Here, final x=y is defines at method level.
Can you clear my doubt please.
 
agilemanoj kumar
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Just assume, final x=y means x has become constant for that method and it wont be changed anymore.
Now inner class is using this variable "x" which is nothing but a constant. So, even after method completion, that constant will still be with inner class. You have to treat final x a constant... That s all...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic