Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

final variable scope

 
Harshana Dias
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have seen in a article mentioned,

Anonymous inner classes have some restrictions in place to prevent breaking encapsulation and to enforce scope. The primary restriction is that an anonymous inner class cannot access variables of the method in which it resides unless those variables are declared as final. This is because the inner class could continue functioning beyond the life of the class and method in which it nests. The variables of the wrapping class would be destroyed once its life was over, so the references the inner class held would become instantly invalid. The only exception to this is a final variable, because it will exist beyond the instance.


My doubt is how come final variable will exist beyond its instance. If so those variables would been static variables right?
 
Henry Wong
author
Marshal
Pie
Posts: 21123
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harshana Dias wrote:I have seen in a article mentioned,

Anonymous inner classes have some restrictions in place to prevent breaking encapsulation and to enforce scope. The primary restriction is that an anonymous inner class cannot access variables of the method in which it resides unless those variables are declared as final. This is because the inner class could continue functioning beyond the life of the class and method in which it nests. The variables of the wrapping class would be destroyed once its life was over, so the references the inner class held would become instantly invalid. The only exception to this is a final variable, because it will exist beyond the instance.


My doubt is how come final variable will exist beyond its instance. If so those variables would been static variables right?


It doesn't. Local variables exist at method scope -- and making those variables final doesn't change the scope.

What is happening is... when the inner class is instantiated, if the local variable is in scope, is declared final, and has already been initialized, then a copy of the value is made for the inner class instance.

Henry
 
Harshana Dias
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


It doesn't. Local variables exist at method scope and making those variables final doesn't change the scope.

What is happening is... when the inner class is instantiated, if the local variable is in scope, is declared final, and has already been initialized, then a copy of the value is made for the inner class instance.

Henry


So because of the variable is final, the value can not be changes inside the inner class and it makes the data consistency.
But still below underline sentence is incorrect know?

The variables of the wrapping class would be destroyed once its life was over, so the references the inner class held would become instantly invalid. The only exception to this is a final variable, because it will exist beyond the instance.
 
Henry Wong
author
Marshal
Pie
Posts: 21123
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harshana Dias wrote:
So because of the variable is final, the value can not be changes inside the inner class and it makes the data consistency.
But still below underline sentence is incorrect know?

The variables of the wrapping class would be destroyed once its life was over, so the references the inner class held would become instantly invalid. The only exception to this is a final variable, because it will exist beyond the instance.


Perhaps you can contact the author of the article to ask -- as it is really unfair of us to comment considering that you can be taking it completely out of context.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic