• 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

Question about inner class

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, In the chapter about inner class in K/B book, it says a Method local inner object can't use the local variables of the method the inner class is in. But if you mark the local variable final, it will work. Could anyone explain a little bit? Thanks!
 
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
Hi Wil,

Welcome to JavaRanch!

Consider the "life cycle" of a method: It's called, it executes, and it exits.

While executing, local variables might be created within the method. But since the scope of these variables is limited to the method, they are typically destroyed when the method exits.

Now suppose that a class is defined within the method. It's certainly possible that an instance of that class will outlive the method in which it's defined. And if that instance is accessing local method variables, there will be a problem when the method exits and those variables no longer exist.

On the other hand, if those method variables are final, then their values can be copied into the instance. That way, when the method exits and local variables are destroyed, copies of their values will remain accessible within the instance. But this is only feasible if the variables are final, because otherwise there would be no guarantee that values copied into the instance would remain correct.
[ January 09, 2006: Message edited by: marc weber ]
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank u for the question aswell as for the ans...learnt a new thing
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic