Possible Duplicates:
Cannot refer to a non-final variable inside an inner class defined in a different method
Why inner classes require “final” outer instance variables [Java] ?
class MyOuter {
private String x = "Outer";
void doStuff(){
final String z = "local variable";
class MyInner {
public void seeOuter(){
System.out.println("Outer x is" + x);
System.out.println("Local variable z is" + z); // does
// not compile if final keyword from String z is removed
}
}
}
}
The above code works fine. I want to know why does the compiler give an error if I remove the final keyword from String z. What difference is the final keyword making?