java singeleton

Read this artitle first

The Most Popular solution is Bill Pugh singeleton.
Bill Pugh solution has:

  • private constructor
  • private static inner Holder class ; Holder has a private static final INSTANCE
  • public static getInstance()

Example:

package org.cfig.learn;

import java.io.Serializable;

/**
 * Created by yu on 1/14/16.
 */
public class BPsingeleton implements Serializable {
    private static final long serialVersionUID = 8382046576562889523L;

    private BPsingeleton() {
    }

    public static BPsingeleton getInstance() {
        return Holder.INSTANCE;
    }

    private static class Holder { //static inner class
        private static final BPsingeleton INSTANCE = new BPsingeleton();
    }

    protected Object readResolve() {
        return getInstance();
    }
}