Java-關於static

來源:互聯網
上載者:User

 private PassParam paramObj = new PassParam();前面沒有必要加private,因為paramObj在範圍終點就消失了。
 Java對象不具備和基本類型一樣的生命週期,當用new建立一個Java對象時,它可以存活於作用之外
 {
  String s = new String("a String");
 }
 引用s在範圍終點就消失了。然而s指向的String對象繼續佔據記憶體空間(記憶體流失),直至"記憶體回收行程"將其回收。
 static StaticTest
 {
  static int i = 47;
  StaticTest st1 = new StaticTest();
  StaticTest st2 = new StaticTest();
  StaticTest.i++;
 }
 StaticTest.i和任意StaticTest對象指向同一份儲存空間,它們共用同一個i的堆地址。StaticTest.i++執行完畢,st1.i,st2.i,StaticTest.i的值全變為48.
 static欄位對每個類只有一份儲存空間,而非static類對每個對象有一份儲存空間.static方法重要用處在於不建立對象的前提下就可以調用它.
 希望無論是否產生對象or產生了多少個對象,某些特定資料在記憶體裡只有一份時,使用static欄位.eg:所有中國人都有個國家名稱,每個中國人都共用這個國家名稱.
 
 當申明一個事物是static,就意味著這個資料和方法不會與包含它的那個類的任何對象執行個體關聯在一起;而非static事物必須知道它們一起運作的特定對象.
 所以對於static方法,不能通過調用其他非static成員或方法而沒有指定某個命名物件,來直接存取非static成員或方法.
 
 在靜態方法裡只能直接調用其它的靜態成員(變數,對象和方法),而不能直接存取類中的非靜態成員(同類中調用不用加類名;其它類中調用:類名.靜態成員or對象名.靜態成員).
 因為對於非靜態成員,需要先建立類的執行個體對象後才可使用,而靜態方法在使用前不用建立任何對象.
 靜態方法不能以任何方式引用this和super關鍵字,理由同上,當靜態方法被調用時,this所引用的對象根本就沒有產生.
 
 System:類名,out:靜態對象,println:out是PrintStream類的static對象,println是PrintStream類的方法.
 
 相反,非靜態方法裡可以調用任何的靜態方法和非靜態方法

 

class Chinese...{
    static String country = "CHINA";
    static int count = 0;
    String name;
    int age;
    
    Chinese(String name,int age)...{
        this.name = name;
        this.age = age;
        count++;//用於統計Chinese總共產生了多少個對象(count為static的,放在構造方法中,很巧妙).
    }
    
    void singOurCountry()...{
        System.out.println(name + " is singing our country:" + country);
    }
}

class TestChinese...{
    public static void main(String[] args)...{
        Chinese person1 = new Chinese("張三",20);
        Chinese person2 = new Chinese("李四",30);
        Chinese person3 = new Chinese("王五",40);
        
        System.out.println("All chinese is singing our country:" + Chinese.country);
        person1.singOurCountry();
        person2.singOurCountry();
        person3.singOurCountry();
    }
}

 

class PassParam1...{
    int i;
    PassParam1 paramObj = new PassParam1();
    public static void main(String[] args)...{
        paramObj.i = 500; //Cannot make a static reference to the non-static field paramObj
    }
}

main方法是static的,如果沒有指定對象,就不能調用非static成員PassObj.修改:
 方法1
 static PassParam1 paramObj = new PassParam1();
 public static void main(String[] args){paramObj.i = 500}
 方法2
 PassParam1 paramObj = new PassParam1();
 public static void main(String[] args)
 {
      PassParam1 Obj = new PassParam1();
      Obj.paramObj.i = 500;
 }
 方法3
 public static void main(String[] args)
 {
      PassParam1 paramObj = new PassParam1();
      paramObj.i = 500;
 }

 

class PassParam2...{
    int i;
    public static void main(String[] args)...{
        PassParam2 paramObj2 = new PassParam2();
        paramObj2.i = 500;
        changeParam(paramObj2.i);
        /**//*Cannot make a static reference to the non-static method changeParam(int) from the type PassParam2
         * 對於static方法,不能通過調用其他非static方法而沒有指定某個命名物件
         * 對於非static方法,可以通過調用其他非static方法而沒有指定某個命名物件,非static方法在使用前是要求執行個體化的*/
        changeParam(paramObj2);
    }
    public void changeParam(int param)...{
        param = 30;
    }
    public static void changeParam(PassParam2 paramObj)...{
        paramObj.i= 300;
    }
}

 

class PassParam3...{
    int param;
    public static void main(String[] args)...{
        param = 50;
        /**//*Cannot make a static reference to the non-static field param*/
    }
}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.