爲什麼我需要聲明local variable
爲final
如果在方法中定義的我的Inner class
需要使用它?內部類和局部變量
實施例:
class MyOuter2 {
private String x = "Outer2";
void doStuff() {
final String y = "Hello World";
final class MyInner {
String z = y;
public void seeOuter() {
System.out.println("Outer x is "+x);
System.out.println("Local variable is "+y);
MyInner mi = new MyInner();
mi.seeOuter();
}
}
}
}
爲什麼字符串y
需要是一個最終恆定?它如何影響?
看看這個討論是否有助於http://techtracer.com/2008/04/14/mystery-of-accessibility-in-local-inner-classes/ – kosa 2012-04-26 17:52:59
可能的重複[無法引用內部的非最終變量在不同的方法中定義的內部類](http://stackoverflow.com/questions/1299837/cannot-refer-to-a-non-final-variable-inside-an-inner-class-defined-in-a- differen) – 2012-04-26 17:53:59
[關於Java中局部最終變量的問題](http://stackoverflow.com/questions/5947352/question-about-local-final-variable-in-java)。 – Lion 2012-04-26 18:33:29