Java Singleton Pattern

  • + 0 comments

    I think Bill Pugh Singleton Implementation is best implementation for singleton.

    public class BillPughSingleton {
    	
    	private BillPughSingleton()
    	{
    		// private constructor
    	}
    	
    	 private static class SingletonHelper{
    	        private static final BillPughSingleton INSTANCE = new BillPughSingleton();
    	    }
    	    
    	    public static BillPughSingleton getInstance(){
    	        return SingletonHelper.INSTANCE;
    	    }
    }
    

    You need to take care of Serialization,reflection and cloning as well. More on this.Singleton design pattern