• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

local inner class

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does a local inner class which resides inside a static method still have the "can only access local variables that is declared final" restriction?
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A method local inner class can access the local variables that are marked final in the method that the class is defined.

However the method local inner class cannot access the local variables of
the class because the local variables are declared on stack and dies once the method completes execution, If JVM would have allowed access to local
variables then there is a possiblity that the object declared of the class
may be passed outside the method and this case the local variable would have be destroyed and that object would laterd try to modify the variable.
Only final varaibles are allowed because they are final, Compiler would replace the final variables with constants and since final variables cannot be modified its perfectly leagal for JVM to allow the access of final local variables by method local inner class.
Thanks
Deepak
 
Evacuate the building! Here, take this tiny ad with you:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic