Let’s say that I have a function that has the purpose of creating a new instance of any specified class, and then returning it. Perhaps something like this:
public static <T> Class<T> forgeClass(Class<T> classReference) {
return new classReference.getClass();
}
Usage (with Employee class):
Employee foo = forgeClass(Employee.class); // new Employee instance
How can I achieve this?
The key is to use
Class.newInstance;Use as follows…
In case you want to use a more complex initialization routine, e.g.
Employee(String name), you should useClass.getConstructor, followed byConstructor.newInstance.