单例模式之内部静态类

  1. public class Singleton  
  2. {  
  3.     private Singleton(){ }  
  4.       
  5.     public static Singleton getInstance()  
  6.     {  
  7.         return Nested.instance;       
  8.     }  
  9.       
  10.     //在第一次被引用时被加载  
  11.     static class Nested  
  12.     {  
  13.         private static Singleton instance = new Singleton();  
  14.     }  
  15.       
  16.     public static void main(String args[])  
  17.     {  
  18.         Singleton instance = Singleton.getInstance();  
  19.         Singleton instance2 = Singleton.getInstance();  
  20.         System.out.println(instance == instance2);  
  21.     }  
  22. }  
posted on 2015-10-28 19:49  XZhe  阅读(135)  评论(0编辑  收藏  举报