• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

final field in a method

 
salvador rcn
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • 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 ?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic