可能重复:
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] ?的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
}
}
}
}
上面的代码工作正常。 我想知道为什么编译器给出一个错误,如果我从字符串z中删除最后一个关键字。最终的关键字制作有什么不同?