• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regarding Method-local inner classes

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends,

We have a rule that " A method-local inner class cannot access the local variables in the method in which it is declared" but when we make local variables as final we can use them in the method local inner class. Can any body explain what the real difference when we make local varables as final so that they can be used in method-local inner classes.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When a method exits, its local variables are destroyed. But an instance of a local class might continue to exist after the method exits, and if it tried to access a local variable at that point, there would be a problem. On the other hand, if a local variable is final, then its value can be copied into the instance of the local class, and the instance can use it essentially as a constant.
reply
    Bookmark Topic Watch Topic
  • New Topic