my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Method Local inner class 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" Watch "Method Local inner class" New topic
Author

Method Local inner class

Somesh Rathi
Ranch Hand

Joined: Apr 27, 2006
Posts: 36
In case of method local inner class, the inner class object cannot use the local variables of the method the inner class is in.
However if the local variables are marked as 'final',inner class can use those variables. WHY ?
Arno Reper
Ranch Hand

Joined: Mar 14, 2006
Posts: 286
Hello,
because final variable are constant and constant variable exists somewhere else that on the stack like the other methode-variable. I mean when the method is done all the stack is empty and the variable are lost...only the final variable remains, so you can use them in your local inner class because they aren't lost.
i hope this help
arno

[ May 15, 2006: Message edited by: Arno Reper ]

The man who makes no mistakes does not usually make anything<br /> <br />>>> SCJP 5.0 >> SCJD B&S <<< In progress
Somesh Rathi
Ranch Hand

Joined: Apr 27, 2006
Posts: 36
Does it means that constant variable(local in method) doesn't live on stack ? When method ends, corresponding local variables removed from stack,am i right?
Arno Reper
Ranch Hand

Joined: Mar 14, 2006
Posts: 286
Yop you are right, final variables are somewhere else ( on the heap , i think ) . the 'normal' variable live into the stack and die at the method's end
arno
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Right.
Make the local variable "final" -- compiler will automatically transfer the value to a more persistent portion of the computer memory
 
wood burning stoves
 
subject: Method Local inner class