11

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 ?

Community
  • 1
  • 1
EMM
  • 1,812
  • 8
  • 36
  • 58

2 Answers2

13

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.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Tarek
  • 1,904
  • 2
  • 18
  • 36
  • 2
    "as it may out-live the function", am new to java and unable to understand this part, could you please explain a scenario where this could happen?Thanks – Srini Jan 17 '14 at 17:25
  • 2
    Supppose the inner class instance is passed as an method argument and the is stored as instance variable of another Object.So although the method variable dies on completion of METHOD,THE INNER class object outlives the method – Kumar Abhinav Mar 09 '14 at 07:19
  • "because the local class instance must maintain a separate copy of the variable" - do local class instances maintain access to all variables in that scope? Is there anything in the documentation to support this, or any test programs? I mean how do we know that local inner classes get a copy of those local variables? – Solace May 22 '16 at 16:42
4

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.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130