[英]Java local variable accessed from within inner class; needs to be declared final, why it works in NetBeans?
In my Java app, there is a method and it has the following code: 在我的Java应用程序中,有一个方法,它具有以下代码:
JButton Button = new JButton(Content);
Button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
doSomething(Button.getText());
}
});
It compiles and works fine from NetBeans 8.1, but today when I tried to compile the project from command line, it gave me the following error: 它可以从NetBeans 8.1编译并正常工作,但是今天当我尝试从命令行编译项目时,它给了我以下错误:
local variable Button is accessed from within inner class;
从内部类内部访问局部变量Button; needs to be declared final
需要宣布为最终
My question is: why the same code complies and works fine from NetBeans 8.1 but can't compile in command line? 我的问题是:为什么相同的代码可以从NetBeans 8.1兼容并正常工作,但不能在命令行中进行编译?
My command line Java version is: 1.8.0_77 我的命令行Java版本是:1.8.0_77
And my NetBeans Java version is: 1.8.0_65 我的NetBeans Java版本是:1.8.0_65
That shouldn't matter, so why NetBeans doesn't consider it an error? 没关系,为什么NetBeans不将其视为错误?
Edit : Woo, BIG SURPRISE for me, I just did a check on javac : 编辑:噢,对我来说是大惊喜,我刚刚在javac上做了检查:
C:\\Users\\USER>java -version java version "1.8.0_77" C:\\ Users \\ USER> java -version Java版本“ 1.8.0_77”
C:\\Users\\USER>javac -version javac 1.7.0_40 C:\\ Users \\ USER> javac -version javac 1.7.0_40
Wait a minute, I'm 100% confused big time, first I never knew there is a "javac -version" command, I thought "java -verison" will tell me both java and javac, and I assume they are the same, second, when I installed Java, I thought the installer will update it automatically, and javac is part of java, so it should be internally consistent, but it definitely is NOT, so how can my java -version being different from my javac -version ? 等一下,我100%感到困惑,我首先不知道有一个“ javac -version”命令,我以为“ java -verison”会告诉我java和javac,我认为它们是相同的,其次,当我安装Java时,我以为安装程序会自动更新它,并且javac是java的一部分,因此它应该在内部保持一致,但绝对不是,所以java -version与javac -version有何不同? ? In other words how can I trust Oracle to install my java and javac at the same time, if it doesn't do the job, does that mean everytime I update Java, I need to somehow install my javac by hand ?
换句话说,我如何才能信任Oracle同时安装我的java和javac,如果它不能完成工作,这是否意味着每次更新Java时,都需要以某种方式手动安装javac? I've never done that, so why and how could this happen ?
我从来没有做过,所以为什么以及如何发生?
Prior to Java 8, all local variables that need to be accessed in an inner class need to be declared final
. 在Java 8之前,需要在内部类中访问的所有局部变量都必须声明为
final
。 This is due to an implementation detail -- local variables are implemented as hidden instance variables of the inner class. 这是由于实现细节-局部变量被实现为内部类的隐藏实例变量。 If the variable could be changed, then the copy in the inner class would be incorrect.
如果可以更改变量,则内部类中的副本将不正确。
In Java 8, this requirement was relaxed to "final" or "effectively final" (scroll down about 2/3 of the way). 在Java 8中,此要求放宽为“最终”或“有效最终” (向下滚动约2/3)。
However, starting in Java SE 8, a local class can access local variables and parameters of the enclosing block that are final or effectively final .
但是,从Java SE 8开始,本地类可以访问final或有效的final的局部变量和封闭块的参数。 A variable or parameter whose value is never changed after it is initialized is effectively final.
变量或参数的值在初始化后从未更改,实际上是最终的。
The variable doesn't have to be declared explicitly to be final
, it just has to be effectively final -- not explicitly final
, just not changed. 变量不必显式声明为
final
,而只需有效地为final即可-无需显式final
,只需更改即可。
You are not changing Button
, so it's effectively final and it compiles under Java 8. However, Java 7 still required it to be declared explicitly final
so there is a compiler error. 您无需更改
Button
,因此它实际上是final的,并且可以在Java 8下进行编译。但是,Java 7仍要求将其显式声明为final
因此会出现编译器错误。
Fix your path so that javac
is your Java 8 installation. 修正路径,以便
javac
是Java 8安装。 It's possible you have a JRE (no "javac" executable) that is Java 8 but a JDK 1.7 (Java 7) installation, which could explain your versioning output. 您可能有一个JRE(没有“ javac”可执行文件),它是Java 8,但安装了JDK 1.7(Java 7),可以解释版本控制输出。 In that case you would need to download and install the Java 8 JDK (1.8).
在这种情况下,您将需要下载并安装Java 8 JDK(1.8)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.