Create a new instance of a class by calling a constructor with arguments
import java.lang.reflect.Constructor;
/**
* Handy reflection routines.
*/
public abstract class Reflect {
/**
* Create a new instance of a class by calling a constructor with arguments.
*/
public static Object newInstance(String className, Class[] signature, Object[] args)
throws Exception {
Class cls = Class.forName(className);
Constructor constructor = cls.getConstructor(signature);
return constructor.newInstance(args);
}
}
Related examples in the same category