• Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt related to Inner Class  RSS feed

 
Khuzema Dharwala
Ranch Hand
Posts: 61
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,

Can you please tell me why method local inner class can access final local variables of the method.

Thanks...
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Khuzema Dharwala wrote:Can you please tell me why method local inner class can access final local variables of the method.

No idea, other than it makes sense to me from a "scope" standpoint.

TBH, "why" questions like this aren't very productive. Without talking to the person who decided it originally, all you're going to get from a forum like this is our opinion.

Also: I have never once, in 12 years of writing Java, had a need for a method-local inner class. If you find one that just leaps out at you as being "the right way to do it", do tell me.

Winston
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also note that it's not really accessing the variable. It just gets a copy of the variable's value. That's why it only works with final variables. If we were allowed to use non-finals, then when the nested class got the copy of the variable's value, if either it or the method changed that variable, we would get unexpected and unpredictable results.
 
Khuzema Dharwala
Ranch Hand
Posts: 61
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys for your replies....
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!