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的對象時(構造器也看成靜態(tài)方法),或者類的靜態(tài)方法/靜態(tài)域首次被訪問時,Java解釋器必須查找類路徑,以定位ClassInitialization.class
                2. 然后載入class文件,有關靜態(tài)的初始化都會被執(zhí)行,而且只能在首次加載的時候進行一次。在例子中由于靜態(tài)成員Cupboard已經加載了bowl4和bowl5,因此在重新new Cupboard的時候只有Bowl3被加載
                3. 靜態(tài)的初始化順序是按出現順序執(zhí)行的,因此在例子輸出結果中Bowl(1)在Bowl(2)之前,Bowl(4)在Bowl(5)之前
                4. 當用new 創(chuàng)建對象的時候,首先將在堆上為對象分配足夠的存儲空間
                5. 這塊存儲空間會被清零,即自動地把對象中的所有基本類型數據都設置成了默認值,而引用則被設置成了null。例子中的bowl3會先被設置成null(也就是說非static的成員初始化都是在static之后的,因此Bowl(3)在Bowl(4)和Bowl(5)之后)
                6. 編譯器會檢查該類是不是extends于基類,如果是則載入基類。到了基類后,加載順序和1-7一樣
                7. 執(zhí)行所有出現于字段定義處的初始化動作。例中bowl3被賦予指向Bowl3對象的引用
                8. 執(zhí)行構造器。
          posted on 2007-07-11 16:07 Eric-1001c 閱讀(182) 評論(0)  編輯  收藏 所屬分類: ThinkingInJava
          主站蜘蛛池模板: 若尔盖县| 泾川县| 仙游县| 来安县| 迁西县| 绥江县| 临西县| 昌黎县| 治多县| 景宁| 和田市| 洛南县| 华池县| 江永县| 淮南市| 高台县| 博白县| 嫩江县| 清苑县| 筠连县| 广灵县| 西安市| 井陉县| 京山县| 临夏市| 晋中市| 马鞍山市| 盐津县| 客服| 苏尼特左旗| 吴忠市| 三河市| 铁岭市| 枣阳市| 临沧市| 盐边县| 荆门市| 扬州市| 鄂托克前旗| 富裕县| 阿勒泰市|