RSS

Generic Singleton Class for Java

15 Oct
class singleton<T> {
    private static final singleton<?> instance = new singleton<Object>();
    private singleton() {}
    T variable;  

    public static singleton<?> getInstance() {
        return instance;
    }
}   
void test() {
    singleton<String> singletonString = (singleton<String>)singleton.getInstance();
    String s = singletonString.variable;
    singleton<Integer> singletonInteger = (singleton<Integer>)singleton.getInstance();
    Integer i = singletonInteger.variable;
} 

Referenced by: http://www.coderanch.com/t/470696/java/java/Generic-Singleton-Class

Advertisements
 
Leave a comment

Posted by on October 15, 2011 in Java Pro

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: