public class Singleton
{
//构造器私有化
private Singleton(){ }
/**
* 获取对象实例的静态方法
* @return
*/
public static Singleton getInstance()
{
return SingletonHolder.instance;
}
//静态内部类,在第一次被引用时被加载
private static class SingletonHolder
{
private static Singleton instance = new Singleton();
}
public static void main(String args[])
{
Singleton instance1 = Singleton.getInstance();
Singleton instance2 = Singleton.getInstance();
System.out.println(instance1 == instance2);
}
}