eric-1001c

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            3 隨筆 :: 45 文章 :: 12 評論 :: 0 Trackbacks
          1)類成員的初始化
          class Bowl{
              Bowl(
          int marker){
                  System.out.println(
          "Bowl("+marker+")");
              }

              
          void f1(int marker){
                  System.out.println(
          "f1("+marker+")");
              }

          }

          //檢查static成員位置與被調用的順序的關系
          class Table{
              
          static Bowl bowl1 = new Bowl(1);//before constructor
              Table(){
                  System.out.println(
          "Table()");
                  bowl2.f1(
          1);
              }

              
          void f2(int marker){
                  System.out.println(
          "f2("+marker+")");
              }

              
          static Bowl bowl2 = new Bowl(2);//after constructor
          }

          //檢查static和normal成員位置與被調用的順序關系
          class Cupboard {
              Bowl bowl3 
          = new Bowl(3);       // normal field
              static Bowl bowl4 = new Bowl(4); //static field
              Cupboard(){
                  System.out.println(
          "cupboard()");
                  bowl4.f1(
          2);
              }

              
          void f3(int marker){
                  System.out.println(
          "f3("+marker+")");
              }

              
          static Bowl bowl5 = new Bowl(5); //after constructor
          }

          //檢查static與main函數之間被調用順序的關系
          public class ClassInitialization {
              
          public static void main(String[] args) {
                  System.out.println(
          "Creating new Cupboard() in main");
                  
          new Cupboard();
                  System.out.println(
          "Creating new Cupboard()in main");
                  
          new Cupboard();
                  
                  table.f2(
          1);
                  cupboard.f3(
          1);
              }

              
          static Table table = new Table();
              
          static Cupboard cupboard = new Cupboard();
          }
          //output
          Bowl(1)
          Bowl(2)
          Table()
          f1(1)
          Bowl(4)
          Bowl(5)
          Bowl(3)
          cupboard()
          f1(2)
          Creating new Cupboard() in main
          Bowl(3)
          cupboard()
          f1(2)
          Creating new Cupboard()in main
          Bowl(3)
          cupboard()
          f1(2)
          f2(1)
          f3(1)
             
                1. 當首次構建ClassInitialization的對象時(構造器也看成靜態方法),或者類的靜態方法/靜態域首次被訪問時,Java解釋器必須查找類路徑,以定位ClassInitialization.class
                2. 然后載入class文件,有關靜態的初始化都會被執行,而且只能在首次加載的時候進行一次。在例子中由于靜態成員Cupboard已經加載了bowl4和bowl5,因此在重新new Cupboard的時候只有Bowl3被加載
                3. 靜態的初始化順序是按出現順序執行的,因此在例子輸出結果中Bowl(1)在Bowl(2)之前,Bowl(4)在Bowl(5)之前
                4. 當用new 創建對象的時候,首先將在堆上為對象分配足夠的存儲空間
                5. 這塊存儲空間會被清零,即自動地把對象中的所有基本類型數據都設置成了默認值,而引用則被設置成了null。例子中的bowl3會先被設置成null(也就是說非static的成員初始化都是在static之后的,因此Bowl(3)在Bowl(4)和Bowl(5)之后)
                6. 編譯器會檢查該類是不是extends于基類,如果是則載入基類。到了基類后,加載順序和1-7一樣
                7. 執行所有出現于字段定義處的初始化動作。例中bowl3被賦予指向Bowl3對象的引用
                8. 執行構造器。
          posted on 2007-07-11 16:07 Eric-1001c 閱讀(182) 評論(0)  編輯  收藏 所屬分類: ThinkingInJava
          主站蜘蛛池模板: 喀什市| 宁南县| 铅山县| 信阳市| 子长县| 阆中市| 中西区| 当阳市| 阳新县| 巧家县| 福泉市| 思茅市| 济宁市| 双牌县| 团风县| 徐州市| 陕西省| 高淳县| 中江县| 包头市| 年辖:市辖区| 双鸭山市| 深水埗区| 丰宁| 兴化市| 马龙县| 呼图壁县| 宜兴市| 全南县| 靖州| 青铜峡市| 隆尧县| 榆社县| 龙门县| 武陟县| 增城市| 高邮市| 乐都县| 花莲县| 蓝山县| 汝州市|