1 package com; 2 3 public class LazySingletonTest { 4 public static void main(String[] args) { 5 LazySingleton inst1 = LazySingleton.getInstance(); 6 LazySingleton inst2 = LazySingleton.getInstance(); 7 System.out.println(inst1 == inst2); 8 } 9 } 10 11 class LazySingleton{ 12 private static LazySingleton instance; 13 private LazySingleton(){ 14 15 } 16 17 public static LazySingleton getInstance() { 18 if(instance==null){ 19 synchronized (LazySingleton.class) { 20 if(instance==null) 21 instance = new LazySingleton(); 22 } 23 } 24 return instance; 25 } 26 }