博客已轉(zhuǎn)到HuJinPu 's blog

          http://blog.csdn.net/hujinpu

          首頁 新隨筆 聯(lián)系 聚合 管理
            24 Posts :: 0 Stories :: 25 Comments :: 0 Trackbacks

          覺得Core Java在Java 初始化過程的總體順序沒有講,只是說了構(gòu)造器時的順序,作者似乎認為路徑很多,列出來比較混亂.我覺得還是要搞清楚它的過程比較好.所以現(xiàn)在結(jié)合我的學習經(jīng)驗寫出具體過程:

          過程如下:

          1.在類的聲明里查看有無靜態(tài)元素(static element, 我姑且這么叫吧),比如static int? x = 1,
          {//block
          ??????float sss = 333.3; String str = "hello";
          }
          或者 比如
          ?static {? //(static block),
          ????????????int x = 2;
          ????????????double y = 33.3;
          }
          如果有static element則首先執(zhí)行其中語句,但注意static element只執(zhí)行一次,在第二次創(chuàng)建類的對象的時候,就不會去執(zhí)行static element的語句.

          2.查看此類是否為啟動運行類,若為啟動運行類,則執(zhí)行main()方法里的語句對應語句

          3.若不是啟動運行類,則按代碼的排版先后順序繼續(xù)執(zhí)行非static element的變量賦值以及代碼塊.

          4.最后執(zhí)行構(gòu)造方法,如果在被調(diào)用的構(gòu)造方法里面有this關(guān)鍵字(注意,如果你考慮要調(diào)用其他構(gòu)造方法,則應該把this寫在最前面,不然會產(chǎn)生錯誤),則先調(diào)用相應構(gòu)造方法主體,調(diào)用完之后再執(zhí)行自己的剩下語句.

          下面給出一個實例:

          /**
          ?*
          ?* @author livahu
          ?* Created on 2006年9月6日, 下午17:00
          ?*/
          class FirstClass {
          ??? FirstClass(int i) {
          ??????? System.out.println("FirstClass(" + i + ")");
          ??? }
          ???
          ??? void useMethod(int k) {
          ??????? System.out.println("useMethod(" + k + ")");
          ??? }
          }

          class SecondClass {
          ??? static FirstClass fc1 = new FirstClass(1);
          ??? FirstClass fc3 = new FirstClass(3);
          ??? static {
          ??????? FirstClass fc2 = new FirstClass(2);
          ??? }
          ???
          ??? {
          ??????? System.out.println("SecondClass's block, this block is not static block.");
          ??? }
          ???
          ??? SecondClass() {
          ??????? System.out.println("SecondClass()");
          ??? }
          ???
          ??? FirstClass fc4 = new FirstClass(4);
          }

          public class InitiationDemo {
          ??? SecondClass sc1 = new SecondClass();
          ???
          ??? {
          ??????? System.out.println("Hello Java World!");
          ??? }
          ???
          ??? public static void main(String[] args) {
          ??????? System.out.println("Inside main()");
          ??????? SecondClass.fc1.useMethod(100);
          ??????? InitiationDemo idObj = new InitiationDemo();
          ??? }
          ???
          ??? static SecondClass sc2 = new SecondClass();
          }


          運行結(jié)果:

          FirstClass(1)
          FirstClass(2)
          FirstClass(3)
          SecondClass's block, this block is not static block.
          FirstClass(4)
          SecondClass()
          Inside main()
          useMethod(100)
          FirstClass(3)
          SecondClass's block, this block is not static block.
          FirstClass(4)
          SecondClass()
          Hello Java World!


          posted on 2006-09-06 17:21 livahu 閱讀(654) 評論(0)  編輯  收藏 所屬分類: Learning Core Jave 7edition

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 景德镇市| 海兴县| 昌图县| 宁德市| 涞源县| 古田县| 疏附县| 开封市| 元江| 普陀区| 丰城市| 德兴市| 阳西县| 永兴县| 苍溪县| 灵宝市| 梅州市| 新宁县| 庆云县| 宝山区| 通州市| 三河市| 灵山县| 西乌珠穆沁旗| 拉孜县| 弋阳县| 育儿| 湘潭市| 庆城县| 甘肃省| 吉林省| 天等县| 溧水县| 邵阳市| 花莲市| 天台县| 抚州市| 玉山县| 武穴市| 嘉鱼县| 荔波县|