package java06; /* * 先初始化对象称为:饿汉式 * 对象是方法被调用时,才初始化,也叫做对象的延时加载。成为:懒汉式。 */ /** * 饿汉式 */ class Single_A { private static Single_A single_A = new Single_A(); private Single_A() { } public static Single_A getSingle_A() { return single_A; } } /** * 懒汉式 */ class Single_B { private static Single_B single_B = null; private Single_B() { } public static Single_B getInstance() { if (single_B == null) { synchronized (Single.class) { if (single_B == null) single_B = new Single_B(); } } return single_B; } } public class SingleDemo1 { public static void main(String[] args) { } }