为什么我在访问最终局部变量时会出现 Java 中的 InstantiationException?
我正在使用一些代码来制作“类似闭包”的构造(顺便说一句,不起作用)
一切看起来都很好,但是当我尝试访问代码中的最终局部变量时,抛出异常 InstantiationException
。
如果我通过完全删除局部变量或使其成为类属性来删除对局部变量的访问,则不会发生异常。
该文档说: InstantiationException
当应用程序尝试使用类 Class 中的 newInstance 方法创建类的实例,但指定的类对象无法实例化时抛出。实例化可能会因多种原因而失败,包括但不限于:
- 类对象表示抽象类、接口、数组类、基本类型或 void
- 该类没有无效构造函数
还有什么其他原因可能导致此问题?
这是代码。注释/取消注释类属性/局部变量以查看效果(第 5 行和第 10 行)。
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class InstantiationExceptionDemo {
//static JTextField field = new JTextField();// works if uncommented
public static void main( String [] args ) {
JFrame frame = new JFrame();
JButton button = new JButton("Click");
final JTextField field = new JTextField();// fails if uncommented
button.addActionListener( new _(){{
System.out.println("click " + field.getText());
}});
frame.add( field );
frame.add( button, BorderLayout.SOUTH );
frame.pack();frame.setVisible( true );
}
}
class _ implements ActionListener {
public void actionPerformed( ActionEvent e ){
try {
this.getClass().newInstance();
} catch( InstantiationException ie ){
throw new RuntimeException( ie );
} catch( IllegalAccessException ie ){
throw new RuntimeException( ie );
}
}
}
这是 Java 中的错误吗?
编辑
哦,我忘了,堆栈跟踪(抛出时)是:
Caused by: java.lang.InstantiationException: InstantiationExceptionDemo$1
at java.lang.Class.newInstance0(Class.java:340)
at java.lang.Class.newInstance(Class.java:308)
at _.actionPerformed(InstantiationExceptionDemo.java:25)
I was playing with some code to make a "closure like" construct ( not working btw )
Everything looked fine but when I tried to access a final local variable in the code, the exception InstantiationException
is thrown.
If I remove the access to the local variable either by removing it altogether or by making it class attribute instead, no exception happens.
The doc says: InstantiationException
Thrown when an application tries to create an instance of a class using the newInstance method in class Class, but the specified class object cannot be instantiated. The instantiation can fail for a variety of reasons including but not limited to:
- the class object represents an abstract class, an interface, an array class, a primitive type, or void
- the class has no nullary constructor
What other reason could have caused this problem?
Here's the code. comment/uncomment the class attribute / local variable to see the effect (lines:5 and 10 ).
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class InstantiationExceptionDemo {
//static JTextField field = new JTextField();// works if uncommented
public static void main( String [] args ) {
JFrame frame = new JFrame();
JButton button = new JButton("Click");
final JTextField field = new JTextField();// fails if uncommented
button.addActionListener( new _(){{
System.out.println("click " + field.getText());
}});
frame.add( field );
frame.add( button, BorderLayout.SOUTH );
frame.pack();frame.setVisible( true );
}
}
class _ implements ActionListener {
public void actionPerformed( ActionEvent e ){
try {
this.getClass().newInstance();
} catch( InstantiationException ie ){
throw new RuntimeException( ie );
} catch( IllegalAccessException ie ){
throw new RuntimeException( ie );
}
}
}
Is this a bug in Java?
edit
Oh, I forgot, the stacktrace ( when thrown ) is:
Caused by: java.lang.InstantiationException: InstantiationExceptionDemo$1
at java.lang.Class.newInstance0(Class.java:340)
at java.lang.Class.newInstance(Class.java:308)
at _.actionPerformed(InstantiationExceptionDemo.java:25)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嗯,这是有道理的。
只有
_
类的第一个实例有权访问局部变量。后续实例不能,除非你为它们提供它(通过构造函数arg)输出1。(
a
是你的匿名类的实例)也就是说,我不认为这是一个好方法来实施关闭。初始化块至少被调用一次,而不需要它。我假设您只是玩玩,但请看一下 lambdaj。或者等待 Java 7 :)
Well, that makes sense.
Only your first instance of the
_
class has access to the local variable. Subsequent instances can't, unless you provide them with it (via constructor arg)outputs 1. (
a
is the instance of your anonymous class)That said, I don't think this is a good way to implement closures. The initializer block is called at least once, without the need of it. I assume you are just playing around, but take a look at lambdaj. Or wait for Java 7 :)
以下是
static field
版本的javap -c InstantiationExceptionDemo$1
的摘录:这是
的
局部变量版本:javap -c InstantiationExceptionDemo$1
Final这就是您的原因:
final
局部变量版本在构造函数中需要一个额外的参数,即JTextField
引用。它没有无效构造函数。如果你仔细想想,这是有道理的。否则,这个版本的
InstantiationExceptionDemo$1
将如何获取field
引用?编译器隐藏了这样一个事实:这是作为合成构造函数的参数给出的。Here's an excerpt of the
javap -c InstantiationExceptionDemo$1
of thestatic field
version:And here's the
javap -c InstantiationExceptionDemo$1
of thefinal
local variable version:So there's your cause: the
final
local variable version needs an extra argument, theJTextField
reference, in the constructor. It has no nullary constructor.This makes sense if you think about it. Else, how is this version of
InstantiationExceptionDemo$1
going to get thefield
reference? The compiler hides the fact that this is given as a parameter to the synthetic constructor.感谢 Bozho 和 Polygenlubricants 提供的启发性答案。
所以,原因是(用我自己的话说)
当使用局部最终变量时,编译器会使用匿名内部类使用的字段创建一个构造函数并调用它。它还用值“注入”字段。
因此,我所做的就是修改我的创建,以使用反射加载具有正确值的正确构造函数。
这是生成的代码:
当然,正如 Bozho 指出的那样,这不是创建闭包的好方法(不是一种方法,但不是一个好方法)。
这有两个问题。
1.- 初始化块在声明时被调用。
2.- 无法获取实际代码的参数(即actionPerformed 中的actioneEvent )
如果我们可以延迟初始化块的执行,这将是一个很好的(就语法而言)闭包替代方案。
也许在 Java 7 中:(
Thank you both Bozho and Polygenlubricants for the enlightening answers.
So, the reason is ( in my own words )
When using a local final variable, the compiler creates a constructor with the fields used by the anonymous inner class and invokes it. It also "inject" field with the values.
So, what I did, was to modify my creation to load the right constructor with the correct values using reflection.
This is the resulting code:
Of course, as Bozho points out, this is not a good way ( not it is a way, but not a good one ) to create closures.
There are two problems with this.
1.- The initializer block is invoked when it is declared.
2.- There is no way get the parameters of the actual code ( ie actioneEvent in actionPerformed )
If we could just delay the execution of the initializer block this would make a nice ( in terms of syntax ) closure alternative.
Perhaps in Java 7 :(