aspose file tools*
The moose likes Beginning Java and the fly likes final variable scope Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "final variable scope" Watch "final variable scope" New topic
Author

final variable scope

Harshana Dias
Ranch Hand

Joined: Jun 11, 2007
Posts: 327
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
Sheriff

Joined: Sep 28, 2004
Posts: 18978
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Harshana Dias
Ranch Hand

Joined: Jun 11, 2007
Posts: 327


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
Sheriff

Joined: Sep 28, 2004
Posts: 18978
    
  40

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: final variable scope