博客已轉(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)造器時(shí)的順序,作者似乎認(rèn)為路徑很多,列出來比較混亂.我覺得還是要搞清楚它的過程比較好.所以現(xiàn)在結(jié)合我的學(xué)習(xí)經(jīng)驗(yàn)寫出具體過程:

          過程如下:

          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)建類的對(duì)象的時(shí)候,就不會(huì)去執(zhí)行static element的語句.

          2.查看此類是否為啟動(dòng)運(yùn)行類,若為啟動(dòng)運(yùn)行類,則執(zhí)行main()方法里的語句對(duì)應(yīng)語句

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

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

          下面給出一個(gè)實(shí)例:

          /**
          ?*
          ?* @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();
          }


          運(yùn)行結(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 閱讀(650) 評(píng)論(0)  編輯  收藏 所屬分類: Learning Core Jave 7edition

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 尖扎县| 南阳市| 乌审旗| 天门市| 教育| 宁陵县| 普格县| 马尔康县| 松原市| 宁海县| 离岛区| 邻水| 安康市| 如皋市| 横山县| 灌阳县| 敖汉旗| 宁南县| 晋城| 夹江县| 定州市| 老河口市| 全南县| 绥江县| 北安市| 安阳县| 阿瓦提县| 响水县| 三亚市| 堆龙德庆县| 荆门市| 泸西县| 灵山县| 中方县| 三台县| 灵川县| 长治市| 藁城市| 潼关县| 宁远县| 敖汉旗|