1.

public class MySingleton {

  private MySingleton() {}

  private MySingleton instance = new MySingleton();

  public void getInstance() {

    return instance;

  }

}

 

2.

public class MySingleton {

  private MySingleton() {}

  private MySingleton instance = null;

  public synchronized void getInstance() {

    if (instance == null) {

      instance = new MySingleton();

    }

    return instance;

  }

}

 

3.

public enum MySingleton {

  INSTANCE;

}

 


posted on 2016-03-25 17:21  熊麻吉  阅读(170)  评论(0编辑  收藏  举报