Cannot refer to a non-final variable * inside an inner class defined in a different method"错误解析
2014-01-22 22:58
781 查看
在使用Java局部内部类或者匿名内部类时,若该类调用了所在方法的局部变量,则该局部变量必须使用final关键字来修饰,否则将会出现编译错误“Cannot refer to a non-final variable * inside
an inner class defined in a different method” 下面通过一段代码来演示和分析原因。
[java]
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();
}
}
上面代码若去掉第9行和第14行的注释符号,则第14行就会给出“Cannot refer to a non-final variable * inside an inner class defined in a different method”这样的编译错误。原因如下:在方法中定义的变量时局部变量,当方法返回时,局部变量(str1,str2)对应的栈就被回收了,当方法内部类去访问局部变量时就会发生错误。当在变量前加上final时,变量就不在是真的变量了,成了常量,这样在编译器进行编译时(即编译阶段)就会用变量的值来代替变量,这样就不会出现变量清除后,再访问变量的错误。
an inner class defined in a different method” 下面通过一段代码来演示和分析原因。
[java]
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();
}
}
上面代码若去掉第9行和第14行的注释符号,则第14行就会给出“Cannot refer to a non-final variable * inside an inner class defined in a different method”这样的编译错误。原因如下:在方法中定义的变量时局部变量,当方法返回时,局部变量(str1,str2)对应的栈就被回收了,当方法内部类去访问局部变量时就会发生错误。当在变量前加上final时,变量就不在是真的变量了,成了常量,这样在编译器进行编译时(即编译阶段)就会用变量的值来代替变量,这样就不会出现变量清除后,再访问变量的错误。
相关文章推荐
- Cannot refer to a non-final variable * inside an inner class defined in a different method"错误解析
- Cannot refer to a non-final variable * inside an inner class defined in a different method"错误解析
- "Cannot refer to a non-final variable * inside an inner class defined in a different method"错误解析
- annot refer to a non-final variable * inside an inner class defined in a different method"错误解析
- annot refer to a non-final variable * inside an inner class defined in a different method"错误解析
- Cannot refer to a non-final variable * inside an inner class defined in a different method错误问题
- JAVA错误:Cannot refer to a non-final variable * inside an inner class defined in a different method
- JAVA错误:Cannot refer to a non-final variable * inside an inner class defined in a different method
- JAVA错误:Cannot refer to a non-final variable * inside an inner class defined in a different method
- Cannot refer to a non-final variable mylis inside an inner class defined in a different method
- Cannot refer to a non-final variable music inside an inner class defined in a different method
- Cannot refer to a non-final variable mylis inside an inner class defined in a different method
- Cannot refer to a non-final variable music inside an inner class defined in a different method
- Cannot refer to a non-final variable mylis inside an inner class defined in a different method
- Cannot refer to a non-final variable i2 inside an inner class defined in a different method 的真正原因
- Cannot refer to a non-final variable hql inside an inner class defined in a different method
- Cannot refer to a non-final variable mylis inside an inner class defined in a different method
- java 更换皮肤问题Cannot refer to a non-final variable inside an inner class defined in a different method
- Cannot refer to a non-final variable file inside an inner class defined in a different method
- Cannot refer to a non-final variable inside an inner class defined in a different method