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..
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.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
Yogesh Mashalkar
Greenhorn
Joined: Sep 20, 2005
Posts: 25
posted
0
that makes things clear!
thanks for the response
yogi maheshnath
Ranch Hand
Joined: Jan 07, 2008
Posts: 30
posted
0
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.
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
Joined: Apr 16, 2004
Posts: 201
posted
0
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??
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
Joined: Aug 06, 2007
Posts: 486
posted
0
Instance variable is defined at classlevel. Here, final x=y is defines at method level. Can you clear my doubt please.
Thanks<br />Dinesh
agilemanoj kumar
Ranch Hand
Joined: Mar 07, 2008
Posts: 70
posted
0
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...