1. 靜態

來源:互聯網
上載者:User

標籤:data   setname   輸出   非靜態屬性   property   his   變數   使用   修改   

靜態屬性(即靜態變數)

  1. 一個類的某個屬性為靜態時, 該屬性是全域的, 即以該類為模板建立的所有的對象的這個靜態屬性的指向同一個方法區.靜態屬性在使用方式上不依賴於對象.
    靜態屬性是全域的, 該屬性只有一份, 這個只有一份的意思就是:相對於非靜態屬性而言, 每個對象的非靜態屬性都是獨立的, 而每個對象的靜態屬性都是一樣的, 都指向同一片記憶體區. 所以當修改了這個類模板的靜態屬性或者某個對象修改了自己的這個靜態屬性, 對應的所有的對象的該靜態屬性都會更改.
    案例:import lombok.Data;

public class OuterClass {

public static void main(String[] args) {    // 建立對象1    StaticProperty staticProperty1 = new OuterClass.StaticProperty();    staticProperty1.setId(2);    staticProperty1.setName("li");    // 建立對象2    StaticProperty staticProperty2 = new OuterClass.StaticProperty();    staticProperty2.name = "wu";    // 輸出對象1和對象2    System.out.println(staticProperty1);    System.out.println(staticProperty2);}@Datapublic static class StaticProperty {    private int id = 1;    private static String name;    public void setName(String name) {        StaticProperty.name =  name;    }    @Override    public String toString() {        return "StaticProperty{" +                "id=" + id + ", name=" + this.name +                ‘}‘;    }}

}

1. 靜態

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.