马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
在使用Java局部内部类或者匿名内部类时,若该类调用了所在方法的局部变量,则该局部变量必须使用final关键字来修饰,否则将会出现编译错误“Cannot refer to a non-final variable * inside an inner class defined in a different method” 下面通过一段代码来演示和分析原因。public class Example {
public static void main(String args[]) {
doSomething();
}
private static void doSomething() {
final String str1 = "Hello";
// String str2 = "World!";
// 创建一个方法里的局部内部类
class Test {
public void out() {
System.out.println(str1);
// System.out.println(str2);
}
}
Test test = new Test();
test.out();
}
}
我用的是jdk10.1,是否去掉那两句注释,程序都能正常输出,为什么呢?
jdk8优化了,智能判断变量的值是不是真的改了,只要没有修改,final去掉也可以。如果你放开第9行,在第9行后再加一行,修改一下str2的值,14行就会报错:"Local variable str2 defined in an enclosing scope must be final or effectively final"。
|