File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes What does it mean by final method arguments or final local variables in a instance method ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What does it mean by final method arguments or final local variables in a instance method ?" Watch "What does it mean by final method arguments or final local variables in a instance method ?" New topic
Author

What does it mean by final method arguments or final local variables in a instance method ?

Song Guo
Greenhorn

Joined: Apr 24, 2011
Posts: 24




age must be final, but name does not, why ??

Another, the inner class object cannot use the local variables of the method the inner class is in.Unless the local variables are marked final! Why ??
Thank you very much ! please give me some persuasive explanations.


gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

here you are creating a method local anonymous inner class. the reason for this has to do more with the METHOD LOCAL(or generalising block local) than anonymous part. a block local inner class is defined within a block. it can be instance initializer, method. now as you know scope of a local variable is within the block in which it is defined. also the lifetime of that local variable is till the method is on the stack. right. so as soon as method call blows off the stack all the local variables are destroyed. now if a method local class , accesses a local variable then you can see that the object of method local inner class will be created on heap, and the lifetime of the object is till there is any live thread which can access it. whereas the local variable lifetime is limited i.e. till the method is on the stack . when we make a local variable final(age in your case), a reference of age is passed onto the inner class object. so even though method call is completed, local variables are destoryed, inner class object still has reference to age(although not the same age variable).

coming to the second part, since you are not using local variable name in your inner class declaration, you need not modify it with final modifier
Song Guo
Greenhorn

Joined: Apr 24, 2011
Posts: 24

gurpeet singh wrote:here you are creating a method local anonymous inner class. the reason for this has to do more with the METHOD LOCAL(or generalising block local) than anonymous part. a block local inner class is defined within a block. it can be instance initializer, method. now as you know scope of a local variable is within the block in which it is defined. also the lifetime of that local variable is till the method is on the stack. right. so as soon as method call blows off the stack all the local variables are destroyed. now if a method local class , accesses a local variable then you can see that the object of method local inner class will be created on heap, and the lifetime of the object is till there is any live thread which can access it. whereas the local variable lifetime is limited i.e. till the method is on the stack . when we make a local variable final(age in your case), a reference of age is passed onto the inner class object. so even though method call is completed, local variables are destoryed, inner class object still has reference to age(although not the same age variable).

coming to the second part, since you are not using local variable name in your inner class declaration, you need not modify it with final modifier



Making a local variable final means the variable is on the heap instead of stack ?
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

no . making a variable final does not place it on heap. remember variables are created on stack only. there is noway you can put them on heap. only instance variables are on heap through their respective objects.

in context of inner classes , making local variable final just gives reference of that variable to the block local inner class, so that even after the method finishes off it can access that variable through the acquired reference.
Song Guo
Greenhorn

Joined: Apr 24, 2011
Posts: 24

gurpeet singh wrote:no . making a variable final does not place it on heap. remember variables are created on stack only. there is noway you can put them on heap. only instance variables are on heap through their respective objects.

in context of inner classes , making local variable final just gives reference of that variable to the block local inner class, so that even after the method finishes off it can access that variable through the acquired reference.



When the method finished, the method stack frame was destroyted, and the variable age was also disappeared. How access the variable through reference ?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18963
    
  40



This topic seems to be all over the place.... (1) the definition of the final keyword on a declared variable, (2) an issue related to the accessibility of a variable by an inner class, and now, (3) where it is on the heap or the stack.

Maybe it would be a good idea to focus on one, as it is very difficult to juggle like this.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Song Guo
Greenhorn

Joined: Apr 24, 2011
Posts: 24

Henry Wong wrote:

This topic seems to be all over the place.... (1) the definition of the final keyword on a declared variable, (2) an issue related to the accessibility of a variable by an inner class, and now, (3) where it is on the heap or the stack.

Maybe it would be a good idea to focus on one, as it is very difficult to juggle like this.

Henry


I am concerned about the second issue:" (2) an issue related to the accessibility of a variable by an inner class"
The stack frame was destroyed, how to access the variable though the variable was final ? And what function th final is in this case ?
Thank you very much !
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18963
    
  40

Song Guo wrote:
When the method finished, the method stack frame was destroyted, and the variable age was also disappeared. How access the variable through reference ?


I kinda see what is going on. You are trying to speculate on why it works. On why the inner class can access the local variable, when its scope is different from that of the variable.


A final variable is a variable that is set only once, and can't be changed after it is set. That definition doesn't change when it is accessed via an inner class. Nor is there any behavior change -- no stack versus heap changes. Period. You are way over-complicating everything.

A final variable can be accessed by an inner class, if the inner class is instantiated after the variable has been assigned, because a copy is made of the value. Technically, the inner class is not accessing the local variable -- it is accessing a copy of the variable.

Henry

Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

The key issue here seems to be 'why does a variable in a method need to be final in order to be referenced in an inner class?'


The problem here is that the inner class can have a longer life span then the method in which it was created, and therefor longer than the life of the original variable. Since this is the case, the variable from the method is not actually used in the inner class. Instead, the inner class gets a copy of the variable (and the value from the outer method is passed to the inner class via an implicit setter method). Since the inner class gets a copy of the variable, any changes to the outer method's variable's value would cause an inconsistent state where the inner class had a different state than the method. So the variable in the outer method must be made final to prevent it from changing, so there is no inconsistent state associated with the implicit variable copy associated with the inner class.

So why does the name variable not need to be final? It is also being copied to the inner class through a method call (the inner class's constructor)! The difference here is that the copy is explicit: you, through written code, call the method and pass it a variable. So you know the variable being used in the inner class is a different variable than the one in the outer method.


Steve
Song Guo
Greenhorn

Joined: Apr 24, 2011
Posts: 24

Henry Wong wrote:
Song Guo wrote:
When the method finished, the method stack frame was destroyted, and the variable age was also disappeared. How access the variable through reference ?


I kinda see what is going on. You are trying to speculate on why it works. On why the inner class can access the local variable, when its scope is different from that of the variable.


A final variable is a variable that is set only once, and can't be changed after it is set. That definition doesn't change when it is accessed via an inner class. Nor is there any behavior change -- no stack versus heap changes. Period. You are way over-complicating everything.

A final variable can be accessed by an inner class, if the inner class is instantiated after the variable has been assigned, because a copy is made of the value. Technically, the inner class is not accessing the local variable -- it is accessing a copy of the variable.

Henry



You mean if the variable was final, the variable was copied to the inner class and if the variable was not final , it could not copied to the variable ?
I don't understand why marking the local variable as final would make a difference. My understanding is that marking an object reference variable as final means that you can't assign another object to that reference variable, but it has no effect on the variable's scope.
(My mother tongue is not English, so my statement may be ...)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18963
    
  40

Song Guo wrote:

I am concerned about the second issue:" (2) an issue related to the accessibility of a variable by an inner class"
The stack frame was destroyed, how to access the variable though the variable was final ? And what function th final is in this case ?
Thank you very much !


To add a little color, the JLS only defines the requirements for this.... see section 8.1.3.

http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.1.3

specifically...

Any local variable, formal parameter, or exception parameter used but not declared in an inner class must be declared final.

Any local variable used but not declared in an inner class must be definitely assigned (§16) before the body of the inner class.


but of course, you can easily speculate that this is to allow a copy to be made. If the variable has already been set, and it can't change anymore, then it can only have one possible value, even if the variable that previously held it is out of scope.

Henry
Song Guo
Greenhorn

Joined: Apr 24, 2011
Posts: 24

Henry Wong wrote:
Song Guo wrote:

I am concerned about the second issue:" (2) an issue related to the accessibility of a variable by an inner class"
The stack frame was destroyed, how to access the variable though the variable was final ? And what function th final is in this case ?
Thank you very much !


To add a little color, the JLS only defines the requirements for this.... see section 8.1.3.

http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.1.3

specifically...

Any local variable, formal parameter, or exception parameter used but not declared in an inner class must be declared final.

Any local variable used but not declared in an inner class must be definitely assigned (§16) before the body of the inner class.


but of course, you can easily speculate that this is to allow a copy to be made. If the variable has already been set, and it can't change anymore, then it can only have one possible value, even if the variable that previously held it is out of scope.

Henry

Ok, Thank you.
But I don't understand why marking the local variable as final would make a difference. My understanding is that marking an object reference variable as final means that you can't assign another object to that reference variable, but it has no effect on the variable's scope.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18963
    
  40

Song Guo wrote:
Ok, Thank you.
But I don't understand why marking the local variable as final would make a difference. My understanding is that marking an object reference variable as final means that you can't assign another object to that reference variable, but it has no effect on the variable's scope.



As already mentioned, by more than one person in this thread -- making a variable final has no affect on the scope. We are in 100% agreement with you. This is why a copy is needed.

Henry
Song Guo
Greenhorn

Joined: Apr 24, 2011
Posts: 24

Henry Wong wrote:
Song Guo wrote:
Ok, Thank you.
But I don't understand why marking the local variable as final would make a difference. My understanding is that marking an object reference variable as final means that you can't assign another object to that reference variable, but it has no effect on the variable's scope.



As already mentioned, by more than one person in this thread -- making a variable final has no affect on the scope. We are in 100% agreement with you. This is why a copy is needed.

Henry



I understand!
If the variable were not final. Either the original value or the copied value could change. "There will be some unexpected data synchronization problems. "
In order to prevent this kind of problem, We should mark local variables final.
Thany you very much !
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What does it mean by final method arguments or final local variables in a instance method ?