public class TestNewInstance {
private int id;
private String nm;
public TestNewInstance(int id, String nm) {
super();
this.id = id;
this.nm = nm;
}
public static void main(String[] args) {
try {
TestNewInstance.class.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
private int id;
private String nm;
public TestNewInstance(int id, String nm) {
super();
this.id = id;
this.nm = nm;
}
public static void main(String[] args) {
try {
TestNewInstance.class.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
--------------------
结果
java.lang.InstantiationException: test.TestNewInstance
at java.lang.Class.newInstance0(Class.java:340)
at java.lang.Class.newInstance(Class.java:308)
at test.TestNewInstance.main(TestNewInstance.java:19)