package org.test;public class Singleton { public static void main(String[] args) { // TODO Auto-generated method stub Singleton singleton=Singleton.getInstance(); System.out.println(singleton); } //延迟初始化占位类替代双重检验加锁以达到延迟初始化和线程安全的目的。 private static class SingletonHolder{ public static Singleton singleton=new Singleton(); } public static Singleton getInstance(){ return SingletonHolder.singleton; } private String name="test"; private Singleton(){ } public String toString(){ return name; }}