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)
this.getClass().newInstance()
– Wrafstatic
and the former doesn't. The effect, is, the initializer block is executed before the constructor. It is not widely used because there's constructors in first place, while thestatic {}
construct is the alternative to initialize class resources because there are not "class" constructors. – Wraf