I’ve got this class:
public class A<T> {
protected T something = new T();
...
}
Of course new T() is not possible. What can I do instead?
I must not change the code where the constructor of this class is called, because this is done via reflection.
Annother problem is how to get the Class object of a generic class. mymethod(A.class) worked, but now A has got the parameter T.
You can receive the T as a parameter of the constructor:
Or, if the goal of A is to really create new T instances, then take a factory of T as an argument:
Class<T>can be viewed as aFactory<T>, since it has anewInstance()method, that invokes the public no-arg constructor. But aFactory<T>could create new instances using something other than this constructor.