• 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

problem in method local inner classes ?

 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm studying inner classes from kb6 book and i have a doubt regarding inner classes. the book says that method local inner classes cannot access local variables of the method because local variables live on stack and they wont be as long available as the object of the inner class itself and hence the reason. however it further says that if we mark the local variables final , then the object of the method local inner class can access the local variable. i would like to know why ? after putting final still the local variable is local variable and its scope and lifetime still will be same as it was before putting final and still would live less than method local inner class object. Please explain ?


following are few lines from the book itself.

Because the local variables aren't guaranteed to be alive as long
as the method-local inner class object, the inner class object can't use them. Unless
the local variables are marked final! The following code attempts to access a local
variable from within a method-local inner class.

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Please search first, as this questions comes up often.... BTW, here the first one that I found.

https://coderanch.com/t/572124/java/java/local-variable-scope

Henry
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic