• 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 and final objects

 
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does the inner class requires that all the objects that it tries to access outside the inner class to be "final". Why SuN has introduced this restriction in Java ?
 
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
Because the local variables of one method can't be accessed by another method -- this is an architectural restriction that can't be broken. So when an anymous inner class object is created, what happens "behind the scenes" is that the anonymous object has some member variables that are initialized to have the same values as the local variables the inner class is using. The inner class code really uses these member variables.
If the locals were not final, then you could tell that this is what was happening -- i.e., you could change the locals without the anonymous class noticing, or vice-versa. This restriction is there, then, basically to maintain the illusion that the anonymous class is accessing the local variables of the containing method.
 
Purushoth Thambu
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply... But still i couldn't understand the second part of your reply "if local are not final"... can you give an example ?...
 
Purushoth Thambu
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey thanks ... i got the point what you meant...
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excerpt form SYBEX Java 2 Certification:
�Any variable, either local variable or a formal parameter can be accessed by methods within an inner class provided that variable is marked final. A final variable is effectively a constant so this might seem to be a quite severe restriction, but the point is simply this: An object created inside a method is likely to outlive the method invocation. Because local variables and method arguments are conventionally destroyed when their method exists, these variables would be invalid for access by inner class methods after the enclosing method exits. By allowing access only to final variables, it becomes possible to copy the values of those variables into the object itself, thereby extending their lifetimes.�
>R
 
reply
    Bookmark Topic Watch Topic
  • New Topic