1.public class Singleton { ide
private static class SingletonHolder { ui
public static Singleton resource = new Singleton(); spa
public static Singleton getResource() { orm
return SingletonHolder.resource ; 對象
private Singleton(){ get
2.public class Singleton { it
public static final Singleton INSTANCE = new Singleton(); io
private Singleton(){} class
public void method(){ 方法
public static void main(String[] a){
//調用方法。
Singleton.INSTANCE.method();
3.* 利用枚舉巧妙建立單實例
public enum Singleton {
INSTANCE;http://www.huiyi8.com/jiaoben/
public void method(){ 網頁特效代碼
public static void main(String[] a){
//調用方法。
Singleton.INSTANCE.method();
public class Singleton {
4. private static volatile Singleton instance = null;
* 防止其餘人new對象
private Singleton(){
System.out.println("init");
public static Singleton getInstance(){
if(instance == null){
synchronized(Singleton.class){
if(instance == null){
instance = new Singleton();
return instance;