jeudi 14 avril 2016

reflection in java to create a new instance, getting InstantiationException

I am trying to do something similar to the following:

Using reflection in Java to create a new instance with the reference variable type set to the new instance class name?

I have default constructor that takes no arguments, but I still get an InstantiationException on the line (InterfaceType)c.newInstance();

 Class c = null;
 try {
 c = Class.forName("com.path.to.ImplementationType");
 } catch (ClassNotFoundException e) {
 e.printStackTrace();
 }

 InterfaceType interfaceType = null;
 try {
interfaceType = (InterfaceType)c.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}





Aucun commentaire:

Enregistrer un commentaire