This code is illegal:
class Factory<T> {
public T giveNew() {
return new T();
}
}
because "T" is a ParameterType, not a Class.
But I wonder why this wasn't considered in the design of generics as the compiler should put the real class in the parametrized class and then call newInstance() using that field (hidden to the programmer).
Is there any other way to do this which I don't see?
At the moment, the only way I see this could work is to instantiate the "generic factory" with a real object you want to use and then "clone" that. But this is weird, as then this is no longer a true factory. |