Почему мне нужно объявлять 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
должна быть конечной константой? Как это влияет?