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:

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: Logo

You are commenting using your 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: