Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method local inner class can access final local variable only.Why? [duplicate]

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 ?

like image 999
EMM Avatar asked Oct 09 '11 12:10

EMM


People also ask

Can the method local inner class object Access method local variables?

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.

Can local inner class access non final local variables?

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.

What local variables can a local inner class use?

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.

Is method local inner class must be marked final?

Option A is incorrect because a method-local inner class does not have to be declared final (although it is legal to do so).


2 Answers

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.

like image 164
Tarek Avatar answered Sep 23 '22 12:09

Tarek


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.

like image 25
Peter Lawrey Avatar answered Sep 23 '22 12:09

Peter Lawrey