aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Method-local inner class & final variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Method-local inner class & final variables" Watch "Method-local inner class & final variables" New topic
Author

Method-local inner class & final variables

Yogesh Mashalkar
Greenhorn

Joined: Sep 20, 2005
Posts: 25
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

Joined: Aug 31, 2004
Posts: 11343

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
that makes things clear!

thanks for the response
yogi maheshnath
Ranch Hand

Joined: Jan 07, 2008
Posts: 30
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.


Thanks&Regards<br />Yogi MaheshNath
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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
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??


SCJP 96% | SCWCD 90%| SCJP mock exams | My SCJP blog
Matthew Walton
Greenhorn

Joined: Dec 18, 2007
Posts: 12
The Java runtime takes care of the copying, I believe at the time the method of the inner class is invoked.


What do you mean, 'only String gets to have overloaded operators'?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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


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...


Manoj Kumar
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method-local inner class & final variables