【发布时间】:2015-12-07 14:49:34
【问题描述】:
在学校测试中,我在匿名内部类中使用了非最终变量。 在学校计算机和我的私人计算机上(使用 x86 jre1.8.0_45)它正在工作。
但是,在老师的笔记本电脑上 Eclipse 显示错误(变量应该使用 final)。他使用的是jre1.8.0.x版本(不知道具体版本)。
任何想法为什么它在我的计算机上运行而不是在他的计算机上运行?
在此代码示例中,在 ActionListener 的 actionPerformed 函数中使用了 no final 对象 jLabel:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
public class Main {
public Main(String[] args) {
JLabel jLabel = new JLabel();
JButton button = new JButton();
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
jLabel.setText("xyz");
}
});
}
}
【问题讨论】:
-
听起来很奇怪,据我所知应该是字段变量或声明为 final
-
是警告还是错误?
-
听起来他已经将 eclipse 配置为目标(编译到)java 1.7。即使您安装了 java 1.8,这也是可能的,因为 eclipse 包含自己的编译器
标签: java