1 public class test_test { 2 private test_test(){} 3 private static final test_test instance=new test_test(); 4 public static test_test getInstance(){ 5 return instance; 6 } 7 8 } 9 10 public class test_test{ 11 private test_test(){} 12 private volatile static test_test instance=null; 13 public static test_test getInstance(){ 14 if(instance==null){ 15 synchronized(test_test.class){ 16 if(instance==null){ 17 return new test_test(); 18 } 19 } 20 } 21 } 22 } 23 24 public class test_test{ 25 private test_test(){} 26 //Java的静态内部类 27 private static class test_test setInstance(){ 28 private static final test_test instance=new test_test(); 29 } 30 public static test_test getInstance(){ 31 return setInstance.instance; 32 } 33 }