• 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

Anonymous inner class

 
Ranch Hand
Posts: 628
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Inside an anonymous inner class when we use some object that is defined outside the anonymous inner class the argument reference need to be always final...What is the reason for this....?Am not very clear...
 
Rambo Prasad
Ranch Hand
Posts: 628
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please do clarify ....
 
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rambo Prasad:
Inside an anonymous inner class when we use some object that is defined outside the anonymous inner class the argument reference need to be always final...What is the reason for this....?Am not very clear...



Rambo,

You may found this usefull..

Inner Class
or you can check this more.....

Inner Classes Specification
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If an inner class defined in a method accesses local variables defined in that method, then those local variables must be final. Final variables, of course, have a single value that never changes. This means you can make a copy of them at any time and you won't be able to tell that you're using a copy instead of the original. That's what happens here: copies of those final local variables are made and stored in instance variables of the inner class.

The reason for this is that the method that creates the object may return long before the object is no longer being used. Therefore, without this restriction, the object might try to access local variables that no longer existed (since local variables go away as soon as their defining method returns). By making the variables final and taking a copy when the object is created, the problem is avoided.
 
Shaan Shar
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great Explanations EFH
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic