繁体   English   中英

Java中不止一个类的静态实例?

[英]More than one static instance of a class in Java?

我是新的请不要介意,如果你发现问题愚蠢。我正在搞乱单身代码。我改变了一点(我的问题与单身无关,是的,我已经删除了单实例检查)。我的问题是虽然是一个类java中的实例只能是一个为什么输出中有两个静态类“instance”(参见hash)。我知道“new”关键字会给出一个新的内存地址(那就是用哈希打印的内容)但不是静态类实例应该是一个?所以我得到两个哈希用于打印对象实例,静态变量k具有相同的值,这很好。

public class Singleton {

    private  static Singleton instance;
    static int k;


    public static Singleton getInstance(){
        try{
            instance = new Singleton();

            System.out.println(instance);
        }catch(Exception e){
            throw new RuntimeException("Exception occured in creating singleton instance");
        }
        return instance;
    }

    public static void main(String[] ar) {
        Singleton c1=Singleton.getInstance();
        c1.k=1;
        Singleton c2=Singleton.getInstance();
        c2.k=2;
        System.out.println(c1.k);
        System.out.println(c2.k);

    }
}

输出:

Singleton@15db9742
Singleton@6d06d69c
2
2

您的变量instance在您的两个对象之间共享,但是当您调用instance = new Singleton();时,它指向的对象会被更改instance = new Singleton();

我想你要找的是这个。

public class Singleton {
    public static Singleton instance; //field is made public so we can print it from main class (just to debug)
    static int k;


    public static Singleton getInstance(){
        try{
            instance = new Singleton();
        }catch(Exception e){
            throw new RuntimeException("Exception occured in creating singleton instance");
        }
        return instance;
    }

    public static void main(String[] ar) {
        Singleton c1=Singleton.getInstance();
        c1.k=1;
        Singleton c2=Singleton.getInstance();
        c2.k=2;

        //notice that both of the instance variables of c1 and c2 are printed
        //after we've initialized all of them.
        System.out.println(c1.instance);
        System.out.println(c2.instance);

        System.out.println(c1.k);
        System.out.println(c2.k);

    }
}

在这里,您将获得两个实例变量的相同值

产量

Singleton@6d06d69c
Singleton@6d06d69c
2
2

我们的想法是在为所有对象初始化instance变量后打印值。 最近的初始化将覆盖所有先前的初始化。

你的单身人士不是这样的..

每次调用getInstance时都会生成一个新实例,而不是检查静态对象“ instance ”是否为null。

这就是你得到这个的原因:

Singleton@15db9742 
Singleton@6d06d69c

这清楚地显示了Singleton类的2个实例

如上所述,您的问题在于您不保护实例化。

另外要为@Ghislain Fourny的答案添加更多内容,为了确保在多线程上下文中不实例化2个类,请将方法getInstance添加到关键字“synchronized”。

  public static synchronized Singleton getInstance(){ 
  try{ 
  if(instance == null) { 
      instance = new Singleton();
  }
      System.out.println(instance);
}catch(Exception e){
    throw new RuntimeException("Exception occured in creating singleton instance");
}
return instance;

}

问题是你的getInstance方法总是创建一个新的实例对象。

也就是说,虽然您的实际实例是static ,即类属性,但每次调用getInstance它都会被覆盖。

要解决此问题,您应检查instance是否为null ,然后才创建新对象。

if(instance == null)
  instance = new Stingleton();

return instance;

这是因为缺少一名后卫只能创建一次单身人士。

public static Singleton getInstance(){
  try{
    if(instance == null)
    {
      instance = new Singleton();
    }

    System.out.println(instance);
  } catch(Exception e){
   throw new RuntimeException("Exception occured in creating singleton instance");
  }
  return instance;
}

没有这个保护,仍然只有一个静态实例,但第二次覆盖此实例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM