• 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

regarding inner class

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
i was going through one of the scjp material and for inner class it states that

"A method-local inner class cannot use variables declared within the method (including parameters) unless those variables are marked final."


The reason why the inner class cant access method variables is may be the variables could be cleaned from the stack before the inner class . so this says final has got some different kind of storage system . i would like to know why this different behavior for final variable alone.

thanks in advance

ashok
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know the official reason, but I'll theorise the following:

The local inner class can take an internal copy of the final local variable, because it can be sure that the value will never change. So if it has it's own internal copy, knowing it has a guarantee the value will never change. It won't care whether the local variable is blown away in the stack. The inner class can still function outside the method if a reference to it has been passed to an instance variable or another method.
 
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

Originally posted by Steven Young:
I don't know the official reason, but I'll theorise the following...


Your theory is exactly right!
 
ashok ganesan
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi thanks steve and marc your answer was useful
 
Skool. Stay in. Smartness. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic