posts - 42,comments - 83,trackbacks - 0
                  有人問我一個關于遞歸的問題, 測試代碼如下:

          TestClass.java
           1 public class TestClass {
           2     
           3     private static TestPrinter printer = null;
           4     
           5     static
           6     {
           7         printStaticInfo("initialize test printer in static");
           8         printer = new TestPrinter();
           9     }
          10     
          11     public TestClass()
          12     {
          13         System.out.println("to construct a TestClass object");
          14     }
          15     
          16     public void printOne()
          17     {
          18         printer.printOne();
          19     }
          20     
          21     public static void printStaticInfo(String s)
          22     {
          23         System.out.println(s);
          24     }
          25 }

          TestPrinter.java
           1 public class TestPrinter {
           2 
           3     private static TestClass tc = null;
           4     
           5       static
           6       {
           7           printStaticInfo("initialize test class in static");
           8           tc = new TestClass();
           9       }
          10 
          11     public TestPrinter()
          12     {
          13         System.out.println("to construct a TestPrinter object");
          14     }
          15           
          16     public void printOne()
          17     {
          18         System.out.println("One");
          19         System.out.println("compile again!");
          20     }
          21     
          22     public static void printStaticInfo(String s)
          23     {
          24         System.out.println(s);
          25     }
          26 }


               他的問題是:如果我new一個TestClass對象,然后調用testClass.printOne(),結果是什么? 會不會造成遞歸調用?

              當然不會,因為代碼中的所謂的遞歸都是在static域中的,而static域中的內容只是在這個類裝載的時候調用,也就是說它的調用在constructor之前完成,而且在整個JVM運行期間,static域中的內容只會被執行一次。當然如果這個類在運行期間被GC從PermGen中unload的話,下次該類被裝載的時候,static域中的內容將被重新調用。

               大家可以想想上面的測試輸出結果是什么,不要看下面的答案哦

          initialize test printer in static
          initialize test class in static
          to construct a TestClass object
          to construct a TestPrinter object
          to construct a TestClass object
          One
          compile again!

                  關于static域是在裝載期間還是在對象初始化期間被執行,我們可以通過下面的代碼測試:

           1 public class LoaderTest {
           2     
           3     //to evaluate follow codes is invoked during class loading or object initialized
           4     //class laoding: Class.forName();
           5     //object initialized: Object obj = new ClassName();
           6     static
           7     {
           8         int i = 0;
           9         System.out.println("i is initialized in static during class loading");
          10     }
          11 }

                  我們可以找個jsp,在其中加上如下內容:
          1   try
          2   {
          3         Class.forName("LoaderTest");
          4   }catch(ClassNotFoundException e)
          5   {
          6   }
              
                  好了,上面代碼中我們并沒有去實例化LoaderTest對象,而只是動態裝載這個類,static的內容就被輸出了。
          posted on 2008-09-26 12:38 走走停停又三年 閱讀(2363) 評論(3)  編輯  收藏 所屬分類: Java Technology

          FeedBack:
          # re: 關于Java中的遞歸調用
          2008-09-27 10:44 | YYX
          static塊可以說是Class對象的構造函數  回復  更多評論
            
          # re: 關于Java中的遞歸調用
          2008-09-28 15:26 | zhuxing
          關于初始化有兩個概念:類型初始化和實例初始化^_^  回復  更多評論
            
          # re: 關于Java中的遞歸調用
          2013-08-14 22:38 | maco
          遞歸有沒在詳細的解釋呢  回復  更多評論
            
          主站蜘蛛池模板: 搜索| 商都县| 会东县| 纳雍县| 田林县| 福州市| 屯留县| 台湾省| 贡山| 林口县| 福鼎市| 临清市| 舞阳县| 株洲市| 鹤峰县| 班戈县| 巫山县| 儋州市| 安徽省| 乌拉特中旗| 阳春市| 南安市| 藁城市| 儋州市| 开鲁县| 合山市| 遂川县| 岑溪市| 泽普县| 高密市| 上思县| 惠东县| 安泽县| 永安市| 新昌县| 灵山县| 获嘉县| 苍山县| 许昌县| 穆棱市| 曲阳县|