Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why only final variables can be accesed by local class

 
sushant prabhu
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can someone explain the logic why only final variables can be accessed by a local class and not any non final variables of the method in which the class is declared
 
vinay jain
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason for that is the life span of the objects. The local variables of the method are no longer available after the method finishes but for final variables compiler can generate the necessary code. The local class may exist a lot longer then the local method that's why the local class can't access the automatic variables.
Hope this helps.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic