【发布时间】:2016-10-06 06:30:14
【问题描述】:
内部类只能访问最终或有效的最终变量。虽然我不明白,为什么实例变量无论如何都可以访问,但局部变量和方法参数至少需要有效地最终?
考虑以下代码:
public class BookStore {
private static final int taxId = 300000;
public String name;
public String searchBook(final String criteria) {
int count = 0;
int sum = 0;
// name = ""; I can uncomment this -> no compile error
class Enumerator {
String interate(int k) {
System.out.println(name);
System.out.println(sum);
return "";
}
}
// sum++; If I uncomment this, a compile error will be thrown.
return "";
}
}
为什么局部变量+方法参数必须是有效的final?
【问题讨论】: