posts - 51, comments - 17, trackbacks - 0, articles - 9
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          java static

          Posted on 2007-04-08 20:12 chenweicai 閱讀(130) 評論(0)  編輯  收藏

          1.靜態(tài)變量

          public class Chinese {

           static String country = "中國";
           
           String name;
           int age;
           
           void singOurCountry(){
            System.out.println("啊, 親愛的" + country);
           }
          }

           public static void main(String[] args) {

            System.out.println("Chinese country is :" + Chinese.country);//類.成員
            Chinese ch1 = new Chinese();
            System.out.println("Chinese country is :" + ch1.country);//對象.成員
                  ch1.singOurCountry();
           }


          注:1. 不能把任何方法內(nèi)的變量聲明為static類型
                  2. 靜態(tài)成員變量為該類的各個實(shí)例所共享,所以在內(nèi)存中該變量只有一份,
                      經(jīng)常用來計(jì)數(shù)統(tǒng)計(jì)
          如:
          class A{
               private static int  count = 0;

                public A(){
                    count = count + 1;  
              }

          public void finalize(){//在垃圾回收時(shí),finalize方法被調(diào)用
                 count = count - 1;
              }
          }


          2.靜態(tài)方法
          public class Chinese {

           //在靜態(tài)方法里,只能直接調(diào)用同類中的其他靜態(tài)成員,不能直接訪問類中的非靜態(tài)成員和方法
           //因?yàn)閷θ绶庆o態(tài)成員和方法,需先創(chuàng)建類的實(shí)例對象后才可使用。
           //靜態(tài)方法不能使用關(guān)鍵字this和super,道理同上
           //main方法是靜態(tài)方法,不能直接訪問該類中的非靜態(tài)成員,必須先創(chuàng)建該類的一個實(shí)例
           static void sing(){
            System.out.println("啊...");
           }
           
           void singOurCountry(){
            sing();//類中的成員方法可以直接訪問靜態(tài)成員方法
            System.out.println("啊...祖國...");
           }
          }

           public static void main(String[] args) {

            Chinese.sing();//類.方法
            
            Chinese ch1 = new Chinese();
            ch1.sing();//對象名.方法
            ch1.singOurCountry();
           }


          3. 靜態(tài)代碼塊
          public class StaticCode {

           static String country;
           
           static{
            country = "Chine";
            System.out.println("Static code is loading...");
           }
          }

          public class TestStatic {

           static{
            System.out.println("TestStatic code is loading..."); 
           }
           
           public static void main(String[] args){
            System.out.println("begin executing main method...");
            //類中的靜態(tài)代碼將會自動執(zhí)行,盡管產(chǎn)生了類StaticCode的兩個實(shí)例對象,
            //但其中的靜態(tài)代碼塊只被執(zhí)行了一次。同時(shí)也反過來說明:類是在第一次被使用時(shí)
            //才被加載,而不是在程序啟動時(shí)就加載程序中所有可能要用到的類
            new StaticCode();
            new StaticCode();
           }
           
          }




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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 宁陵县| 高青县| 买车| 和田县| 陆川县| 宁夏| 鸡西市| 定襄县| 大连市| 司法| 昌吉市| 轮台县| 眉山市| 山东省| 繁峙县| 平山县| 宜丰县| 合水县| 杭州市| 惠水县| 霍林郭勒市| 德清县| 江北区| 四子王旗| 兴宁市| 三江| 敦煌市| 和平区| 石林| 任丘市| 凉城县| 额济纳旗| 大连市| 新巴尔虎右旗| 正宁县| 自贡市| 恩施市| 句容市| 类乌齐县| 泸州市| 蛟河市|