The Source for Java Technology Collaboration
User: Password:



Start New Message Delete Post a Reply

Article: 
 Reflection on Tiger
Subject:  A generic Factory
Date:  2004-10-14 23:26:33
From:  carlosayam


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.



 Feed java.net RSS Feeds