| 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
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
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: 32693
|
|
More difficult a question than we usually have here on beginners'.
Moving
|
 |
 |
|
|
subject: Method local Inner Class question
|
|
|