Possible Duplicate:
Why inner classes require “final” outer instance variables [Java]?
Why are only final variables accessible in anonymous class?
class Outer{
private String x = "instance variable";
void doStuff(){
String z = "local variable";
class Inner{
public void seeOuter(){
System.out.println("Outer x is : "+ x);
System.out.println("Local variable z is : " + z); //won't compile
}
}
}
}
Marking the local variable z as final fixes the problem :
final String z = "local variable"; //Now inner object can use it.
Can anyone please explain what is happening ?
I know exactly why it can not compile in case I am trying to access a non-final local variable.
Does making a local-variable final allows it to stay alive even if the method gets completed and local variable goes out of scope?
Does final local variables get stored on a heap instead of stack ?
Yes, we can access the local final variables using the method local inner class because the final variables are stored on the heap and live as long as the method local inner class object may live.
In addition, a local class has access to local variables. However, a local class can only access local variables that are declared final. When a local class accesses a local variable or parameter of the enclosing block, it captures that variable or parameter.
Since the object of an inner class exists within an object of its outer class. Therefore, a local inner class can access the instance variables of its outer class as well as local variables which are in scope.
Option A is incorrect because a method-local inner class does not have to be declared final (although it is legal to do so).
They can use local variables and parameters of the function, but only ones that are declared final
, because the local class instance must maintain a separate
copy of the variable, as it may out-live the function. So as
not to have the confusion of two modifiable variables with
the same name in the same scope, the variable is forced to
be non-modifiable.
Marking the local variable z as final fixes the problem : Can anyone please explain what is happening ?
You have a method local class which is allowed to access final local variable in the scope it is created.
Does making a local-variable final allows it to stay alive even if the method gets completed and local variable goes out of scope?
final means it cannot be changed. Nothing else.
Does final local variables get stored on a heap instead of stack ?
All variables are allocated on the stack, final or not.
I know exactly why it can not compile in case I am trying to access a non-final local variable.
Perhaps you should think about this because its not clear to me that this is the case at all.
A nested class can "access" final variable as these are copied as fields of the object automatically. It does not support non-final fields as they could be changed, either by the method or the class and this is not supported because in reality there are two different fields/variables.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With