美麗涵涵童裝店--說我博客名字,給你們打折!
          隨筆 - 82  文章 - 266  trackbacks - 0
          <2008年10月>
          2829301234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678


          點擊這里給楊愛友發消息
          美麗涵涵童裝店
          說我博客名字,給你們打折!

          常用鏈接

          留言簿(6)

          隨筆分類

          隨筆檔案

          文章檔案

          好友的BLOG

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          下面這段程序的打印結果是:
                  obj.counter1==1
                  obj.counter2==0
          百思不得其解,counter1沒有附初值,默認值也是0啊,為什么他們兩個的結果不一樣呢?
          請大俠講解下其中的原因,謝謝!
          public class Singleton {
            private static Singleton obj = new Singleton();
            public static int counter1;
            public static int counter2 = 0;

            private Singleton() {
              counter1++;
              counter2++;
            }

            public static Singleton getInstance() {
              return obj;
            }

            public static void main(String[] args) {
              Singleton.getInstance();
              System.out.println("obj.counter1==" + counter1);
              System.out.println("obj.counter2==" + counter2);
            }
          }
          posted on 2008-10-28 15:26 楊愛友 閱讀(1392) 評論(9)  編輯  收藏 所屬分類: java相關技術

          FeedBack:
          # re: 類加載問題[未登錄] 2008-10-28 16:38 caoer
          這是因為構造函數在靜態變量初始化前執行:

          Singleton() -> obj.counter1==0 ->obj.counter1==1 ->類變量初始化
          obj.counter2==0 obj.counter2==1
          因此輸出會是obj.counter1==1
          obj.counter2==0  回復  更多評論
            
          # re: 類加載問題 2008-10-28 16:39 日月雨林@gmail.com
          你用過IDE 的debug 功能沒有?一用你就知道為什么了。
          改了一下:
          public class Singleton {

          public static int counter1;
          public static int counter2 = 0;

          private Singleton() {
          counter1++;
          counter2++;
          }

          public static Singleton getInstance() {
          Singleton obj = new Singleton();
          return obj;
          }

          public static void main(String[] args) {
          Singleton.getInstance();
          System.out.println("obj.counter1==" + counter1);
          System.out.println("obj.counter2==" + counter2);
          }
          }
            回復  更多評論
            
          # re: 類加載問題[未登錄] 2008-10-28 16:43 hiker
          類初始化時首先于每一個靜態字段開辟一個存儲區([obj],[counter1=0],[counter2=0])。接著初始化類,所有的靜態成員按照順序初始化(按代碼聲明順序從上到下開始)。故,在初始化obj時候,counter1=1,counter2=1;按照順序,->counter1=1(不變)->counter2=0.  回復  更多評論
            
          # re: 類加載問題 2008-10-28 17:25 jestane
          執行順序問題:
          1.先執行靜態,從上往下,先執行private static Singleton obj = new Singleton();
          2.執行第一步的時候,進入構造函數
          3.在構造函數中count1和count2都被初始化為默認值0,并都++為1.
          4.再執行public static int counter1,只是一個申明,跳過。
          5.最后執行public static int counter2=0,賦值counter2為0.
          6.最后就是輸出的內容了。
            回復  更多評論
            
          # re: 類加載問題[未登錄] 2008-10-28 17:31 henry1451
          private static Singleton obj = new Singleton();

          public static int counter1;
          public static int counter2 = 0;

          把后面兩句放到第一句代碼的前面,就會輸出相同的了,輸出都為1.
          所以@caoer說法有誤碼,正確的應該是:類的初始化順序為,靜態變量(包括類變量和類對象)按按代碼聲明順序從上到下開始,再初始化其他普通變量.  回復  更多評論
            
          # re: 類加載問題[未登錄] 2008-10-29 10:47 james
          Singleton.getInstance() 時候
          開始初始化靜態變量
          當初始化到 private static Singleton obj = new Singleton();
          調用構造函數 兩個counter++
          然后順序繼續靜態變量
          counter1 不變, counter2給了它1
          所以結果是如此  回復  更多評論
            
          # re: 類加載問題 2008-10-29 11:12 depeng@ncsi.com.cn
          public class Singleton {

          public static int counter1;
          public static int counter2 = getINT();

          private static Singleton obj = new Singleton();

          private Singleton() {
          System.out.println("Singleton()" );
          counter1++;
          counter2++;
          }

          public static int getINT(){
          System.out.println("getINT()" );
          return 0;
          }

          public static void main(String[] args) {
          System.out.println("obj.counter1==" + counter1);
          System.out.println("obj.counter2==" + counter2);
          }
          }
            回復  更多評論
            
          # re: 類加載問題 2008-10-29 11:18 depeng@ncsi.com.cn
          getINT()
          Singleton()
          obj.counter1==1
          obj.counter2==1

          ------------------
          賦值的優先權和調用方法一樣
          這里一定要搞清分配空間,和賦值上。 一個是棧,一個是堆
            回復  更多評論
            
          # re: 類加載問題 2008-10-29 11:41 zhuxing
          類型初始化 VS 實例初始化 ^_^  回復  更多評論
            
          美麗涵涵童裝店
          親,說我博客名字,給你們打折!
          主站蜘蛛池模板: 葵青区| 朝阳市| 锡林浩特市| 金溪县| 盐津县| 如东县| 怀集县| 五台县| 恩施市| 昭平县| 杂多县| 乳源| 察隅县| 叶城县| 大石桥市| 静安区| 澳门| 安丘市| 留坝县| 梁平县| 龙南县| 闻喜县| 江安县| 宝清县| 武隆县| 邹城市| 汪清县| 关岭| 雷州市| 屯留县| 保靖县| 河西区| 修武县| 古浪县| 荃湾区| 太保市| 西乌珠穆沁旗| 揭阳市| 南宁市| 邵东县| 扶沟县|