Why Local Inner class requires local variable Final? This Variable is in same method in which Local Inner class define. If we do not use final keyword then it will give compilation error. Please check the following program on ideone
class LocalInnerClassTest2 {
private String x="Outer2";
public static void main(String [] args){
LocalInnerClassTest2 test=new LocalInnerClassTest2();
test.doStuff();
}
void doStuff(){
//Why following local variable "final String z" requires final keyword
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);
}
}
MyInner my=new MyInner();
my.seeOuter();
}
}