Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Generic Singleton

843793Mar 4 2009 — edited Mar 4 2009
Hi guys,
I have been trying to develope a generic singleton pattern and run into a few problems. I have read the previous form comments and I'm still a little confused.
I have the following piece of code:
 private static Hashtable<Class<T>,T> instances = new Hashtable<Class<T>,T>();

    protected Factory(){}

    public static <T> T getInstance(Class<T> cls) 
    {
        T result = null;
   
        if(instances.containsKey(cls))
        {
            
        }
        return result;
    }  
However I know this doesn't work as I can't create a static Object with a generic? I was wondering if someone could show me how it should be done? And maybe post some code ?
David
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 1 2009
Added on Mar 4 2009
6 comments
372 views