Java类如果定义了构造函数 用 class.newInstance()会报异常java.lang.InstantiationException

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();
}
}

}

--------------------

结果

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)

你可能感兴趣的:(java,类,异常,Class)