Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method local Inner Class question

 
aniruddha panadare
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why Method Local inner class can Access local variables marked as "final"?
 
Sachin Adat
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ankit Garg
Sheriff
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 50258
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More difficult a question than we usually have here on beginners'.

Moving
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic