*
The moose likes Java in General and the fly likes Method local Inner Class question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Method local Inner Class question" Watch "Method local Inner Class question" New topic
Author

Method local Inner Class question

aniruddha panadare
Greenhorn

Joined: Dec 23, 2008
Posts: 15
Why Method Local inner class can Access local variables marked as "final"?
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
Because the JLS says so..........

Now, why the JLS says so is because during the execution of your inner class implementation, it creates the objects on the heap.
Now if the inner class object has access to local variables of the method, the objects of the class may use them
even after the method has completed its execution, ie. when they don't reside on the stack.
But, if it is final, its value cannot be changed, so its OK to use the variable, since it will be constant.


SCJP 6
How To Ask Questions On Java Ranch - How To Answer Questions On Java Ranch
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Aniruddha a method local class has a copy of the final constants and parameters of the method. That's why it can access them. This is implemented internally by the compiler. You can read my blog to be more clear. The URL is in my signature


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37874
    
  22
More difficult a question than we usually have here on beginners'.

Moving
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method local Inner Class question
 
Similar Threads
local-method inner class
Doubt on Inner classes
method-local inner class object cannot use the local variables
method local inner class
Method Local inner class