标签:style blog color java ar div sp on c
public class SingletonClass { private static SingletonClass instance=null; public static SingletonClass getInstance() { if(instance==null) { synchronized(SingletonClass.class) { if(instance==null) { instance = new SingletonClass(); } } } return instance; } private SingletonClass(){ } }
将构造函数设为private保证了无法通过构造函数new出SingletonClass对象,只能通过调用public的getInstance方法获得该类的对象(实例),getInstance声明为static类型,在不创建任何对象的前提下,也可以调用该方法。
标签:style blog color java ar div sp on c
原文地址:http://www.cnblogs.com/yongwangzhiqian/p/3991466.html