隨筆-128  評論-55  文章-5  trackbacks-0

          /*   
           *初始化全過程:   
           *   
           *1,    第一次創(chuàng)建MyClass類的對象,或者第一次訪問MyClass的static方法或字段時(shí),Java解釋器會搜尋classpath,找到MyClass.class。   
           *2,    裝載MyClass.class后,會對所有的static數(shù)據(jù)進(jìn)行初始化。這樣第一個(gè)裝載Class對象的時(shí)候,會先進(jìn)行static成員的初始化。   
           *3,    使用new MyClass()創(chuàng)建新對象的時(shí)候,MyClass對象的構(gòu)建進(jìn)程會先在堆里為對象分配足夠的內(nèi)存。 *   
           *4,    清零這塊新內(nèi)存,把MyClass對象的primitive類型的成員賦上缺省值。   
           *5,    執(zhí)行定義成員數(shù)據(jù)時(shí)所作的初始化。    
           *6,    執(zhí)行構(gòu)造函數(shù)。   
           */    
          import static net.mindview.util.Print.*;     
              
          public class Beetle extends Insect     
          {     
              private int k = printInit("Beetle.k initialized");     
              
              public Beetle()     
              {     
                  print("k = " + k);     
                  print("j = " + j);     
              }     
              private static int x2 = printInit("static Beetle.x2 initialized");     
              
              public static void main(String[] args)     
              {     
                  print("Beetle constructor");     
                  Beetle b = new Beetle();     
              }     
          }     
              
          class Insect     
          {     
              private int i = 9;     
              protected int j;     
              
              Insect()     
              {     
                  print("i = " + i + ", j = " + j);     
                  j = 39;     
              }     
              
              private static int x1 = printInit("static Insect.x1 initialized");     
              
              static int printInit(String s)     
              {     
                  print(s);     
                  return 47;     
              }     
          }     
              
              
          /* Output:   
          static Insect.x1 initialized   
          static Beetle.x2 initialized   
          Beetle constructor   
          i = 9, j = 0   
          Beetle.k initialized   
          k = 47   
          j = 39   
          *///:~     
              
          /****************************************************/    
              
          // 變量初始化先后順序的示例     
          import static net.mindview.util.Print.*;     
              
          //當(dāng)創(chuàng)建Window的實(shí)例對象時(shí)會有消息提示     
          class Window     
          {     
              Window(int marker)     
              {     
                  print("Window(" + marker + ")");     
              }     
          }     
              
          class House     
          {     
              Window w1 = new Window(1); // 構(gòu)造函數(shù)前的變量     
              
              House()     
              {     
                  //構(gòu)造函數(shù)里面的變量     
                  print("House()");     
                  w3 = new Window(33); // 重新賦值w3     
              }     
              
              Window w2 = new Window(2); // 構(gòu)造函數(shù)后的變量     
              
              void f()     
              {     
                  print("f()");     
              }     
              
              Window w3 = new Window(3); // 結(jié)束類體時(shí)的對象     
          }     
              
          public class OrderOfInitialization     
          {     
              public static void main(String[] args)     
              {     
                  House h = new House();     
                  h.f();      
              }     
          }      
          /*   
          * 輸出結(jié)果: Window(1) Window(2) Window(3) House() Window(33) f()   
          *   
          * 從結(jié)果看出,雖然域變量w2,w3排在構(gòu)造函數(shù)后面,但它的輸出卻在構(gòu)造函數(shù)前面   
          */    
              
          /****************************************************/    
              
          // 數(shù)組的初始化     
          import java.util.*;     
              
          public class ArrayInit     
          {     
              public static void main(String[] args)     
              {     
                  //直接賦值方式,局限在于數(shù)組在大小編譯確定     
                  Integer[] a = {      
                          new Integer(1),      
                          new Integer(2),      
                          3, // 自動(dòng)包裝     
                          };     
                  //new方式,適于參數(shù)數(shù)量未知的場合,或者參數(shù)類型未知的場合     
                  Integer[] b = new Integer[] {      
                          new Integer(1),      
                          new Integer(2),      
                          3, // 自動(dòng)包裝     
                          };     
                  System.out.println(Arrays.toString(a));     
                  System.out.println(Arrays.toString(b));     
              }     
          }     
          /* 輸出結(jié)果:   
           [1, 2, 3]   
           [1, 2, 3]   
           *///:~   


          Author: orangelizq
          email: orangelizq@163.com

          歡迎大家訪問我的個(gè)人網(wǎng)站 萌萌的IT人
          posted on 2008-12-25 11:30 桔子汁 閱讀(368) 評論(0)  編輯  收藏 所屬分類: J2SE
          主站蜘蛛池模板: 大名县| 罗田县| 齐齐哈尔市| 孟连| 山西省| 乌鲁木齐市| 民乐县| 安岳县| 广宁县| 怀仁县| 黄陵县| 霍林郭勒市| 商南县| 西峡县| 高青县| 友谊县| 深州市| 如东县| 潞城市| 察雅县| 昌乐县| 德钦县| 庆元县| 阿克| 龙江县| 华亭县| 东丰县| 平舆县| 越西县| 天镇县| 正镶白旗| 济源市| 香格里拉县| 永宁县| 额敏县| 迁安市| 霍邱县| 中牟县| 四子王旗| 库伦旗| 固镇县|