使用lazy initialization在第一次调用getReference()是创建对象public static Singleton getReference() { 
  return s; 
}
改为:
public static Singleton getReference() { 
  if(s==null){
     s = new Singleton(47);
  }
  return s; 
}

解决方案 »

  1.   

    public class LazySingleton {  private static LazySingleton lazy = null;
      private int i;
      public LazySingleton() {
      }
      private LazySingleton(int x)
      {
          i=x;
      }  public static LazySingleton getInstance()
      {
          if(lazy==null)
          {
              lazy = new LazySingleton(47);
          }
          return lazy;
      }  public int getValue()
      {
          return i;
      }
      public void setValue(int x)
      {
          i = x;
      }}