簡體   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