• 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

final field in a method

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


"..A class defined within a method can only access final fields of the enclosing method... "


why is this so ? i mean why only the final fields ? what about others ?
 
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 such a class object can have a lifetime longer than the stack frame of the method that created it, the local variables that existed when the object was created may not exist later in the object's life. Therefore, what the Java compiler does behind the scenes is give the class member variables to hold the values of the final local variables and copies the values of the final locals into the members. This gives the illusion that the object is accessing the final locals of the method, but really it's using its own members, which can last longer than the local variables do. The illusion would be spoiled if you could do this with non-final variables, as when their values changed, the member variables would not.
 
reply
    Bookmark Topic Watch Topic
  • New Topic