[問題] static int 問題
最近遇到一個題目,也不知道這該下什麼關鍵字來搜尋。
請問大家,為什麼下面輸出的結果是1及0而不是兩者皆為1? 謝謝~
public class HelloWorld{
public static HelloWorld hw = new HelloWorld();
public static int a1;
public static int a2 = 0;
public HelloWorld(){
a1++;
a2++;
}
public static HelloWorld getInstance() { return hw; }
public static void main(String []args){
HelloWorld obj = HelloWorld.getInstance();
System.out.println(obj.a1);
System.out.println(obj.a2);
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 61.63.106.63
※ 文章網址: http://www.ptt.cc/bbs/java/M.1406291670.A.F22.html
推
07/26 01:27, , 1F
07/26 01:27, 1F
→
07/26 09:42, , 2F
07/26 09:42, 2F
推
07/28 11:33, , 3F
07/28 11:33, 3F