JAVA错误:Cannot refer to a non-final variable * inside an inner class defined in a different method
在使用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();
}
}
上面代码若去掉第9行和第14行的注释符号,则第14行就会给出“Cannot refer to a non-final variable * inside an inner class defined in a different method”这样的编译错误。原因如下:在方法中定义的变量是局部变量,当方法返回时,局部变量(str1,str2)对应的栈就被回收了,当方法内部类去访问局部变量时就会发生错误。当在变量前加上final时,变量就不在是真的变量了,成了常量,这样在编译器进行编译时(即编译阶段)就会用变量的值来代替变量,这样就不会出现变量清除后,再访问变量的错误。
本文转载自:http://www.xue5.com/Developer/Software/717058.html
JAVA错误: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"错误解析
在使用Java局部内部类或者匿名内部类时,若该类调用了所在方法的局部变量,则该局部变量必须使用final关键字来修饰,否则将会出现编译错误“Cannot refer to a non-final va ...
- Cannot refer to a non-final variable inside an inner class defined in a different method
http://stackoverflow.com/questions/1299837/cannot-refer-to-a-non-final-variable-inside-an-inner-clas ...
- 为什么java内部类访问局部变量必须声明为final?
https://blog.csdn.net/z55887/article/details/49229491 先抛出让我疑惑了很久的一个问题 编程时,在线程中使用局部变量时候经常编译器会提示:局部变量必 ...
- Java并发(十九):final实现原理
final在Java中是一个保留的关键字,可以声明成员变量.方法.类以及本地变量. 一旦你将引用声明作final,你将不能改变这个引用了,编译器会检查代码,如果你试图将变量再次初始化的话,编译器会报编 ...
- 【java】关于Cannot refer to the non-final local variable list defined in an enclosing scope解决方法
今天学习中遇到了一个问题: Cannot refer to the non-final local variable list defined in an enclosing scope 这里的new ...
- 基于Windows下处理Java错误:编码GBK的不可映射字符的解决方案
基于Windows下处理Java错误:编码GBK的不可映射字符的解决方案 最近在研究Java,涉及命令行编译,使用notepad++编辑器,然后使用javac编译: 之前的几个文件没有中文的内容,都没 ...
- 【Java学习笔记之二十】final关键字在Java继承中的用法小结
谈到final关键字,想必很多人都不陌生,在使用匿名内部类的时候可能会经常用到final关键字.另外,Java中的String类就是一个final类,那么今天我们就来了解final这个关键字的用法. ...
- 【转】JAVA错误:The public type *** must be defined in its own file***
出现The public type xxx must be defined in its own file这个问题,是由于定义的JAVA类同文件名不一致.public类必须定义在它自己的文件中. 解决 ...
- Java 错误: 找不到或无法加载主类,问题集合
正确编译命令: javac Hello.java 正确运行命令: java Hello 错误1:H:\code>java Hello.java 错误: 找不到或无法加载主类 Hello. ...
随机推荐
- mfs-管理员
http://www.moosefs.org/http://moosefs.com/download.html 两个手册于2015/03/05阅完 moosefs-installation moose ...
- shell算术运算与进制运算
(())与let是等效的 arithmetic expression type 与[是等效的 source与.是等效的 其实,Shell(这里是Bash)本身不具备处理浮点计算的能力,但是可以使用“b ...
- unity, 用unity profiler进行真机profile,需要退出360
用unity profiler进行真机profile,需要退出360.
- 日期转换类 DateConverter.java
package com.util; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.D ...
- java通过http调用服务
package test; import java.io.IOException; import org.apache.commons.httpclient.Cookie; import org.ap ...
- DataRow[] 转为数组
DataRow[] rows = dt.Select("1=1"); ].ToString()).ToArray();
- jquery.find()
http://www.365mini.com/page/jquery-find.htm
- Linux文件系统Ext2,Ext3,Ext4性能大比拼
Linux kernel 自 2.6.28 开始正式支持新的文件系统 Ext4. Ext4 是 Ext3 的改进版,修改了 Ext3 中部分重要的数据结构,而不仅仅像 Ext3 对 Ext2 那样,只 ...
- sqlserver 登录失败——孤立用户
USE (数据库实例)hhwz; GO sp_change_users_login @Action='update_one', @UserNamePattern='数据库用户', @LoginName ...
- 【Linux】之系统工具top
top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器.下面详细介绍它的使用方法.top是一个动态显示过程,即可以通过用户按键来不断刷新 ...