Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Chay Weei Jye

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Singleton

The proper way of implementing a Singleton.

public class MySingleton {
    private static final MySingleton INSTANCE = new MySingleton();

    private MySingleton() {}

    public static final MySingleton getInstance() {
      return INSTANCE;
    }
	 
	/**
	* Normal deserialization returns a new instance of an object. This ensures that only one instance is in existence. 
         * Deserialization can either create a new instance and leave the deserialized object to be garbage collected or
         * reuse the deserialized instance.
	*/
	private Object readResolve() throws ObjectStreamException {
	  return INSTANCE;
	}
}
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS