• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Local variable access issue from inner class.

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Added code tags, please see UseCodeTags for more details]
// page - 671, here y is a stack variable and when method(here method()) ends jvm erase stack entry for
// same but inner class object is still alive in heap so if we wants to access y we need to makes y as final.
// how it works? i.e. does it mean if y is final jvm does not bothers whether y exists or not in memory??
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If y is final then a copy can be safely taken of it and made available to the inner class.

Theoretically, a copy of the value could be taken even if it wasn't final. My guess is that the language designers decided that if it was possible for the local variable to change (after the inner class object was created) then that could lead to confusion as to exactly what value would be used by the inner class.
 
Hey, check out my mega multi devastator cannon. It's wicked. It makes this tiny ad look weak:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic