首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Java 類變量與實例變量差異詳解:static 靜態屬性共享機制與內存佈局

     編輯:Java 相關知識

核心區別

維度類變量 (static)實例變量
關鍵字static無
所屬類對象實例
內存位置方法區堆
生命週期類加載 ~ 類卸載對象創建 ~ GC 回收
共享性所有實例共享一份每個實例獨立一份
訪問方式類名.變量 / 實例.變量只能 實例.變量

代碼演示

public class Demo {
    static int classVar = 0;   // 類變量
    int instanceVar = 0;       // 實例變量
    
    public static void main(String[] args) {
        Demo d1 = new Demo();
        Demo d2 = new Demo();
        
        d1.classVar = 10;      // 等同 Demo.classVar = 10
        d1.instanceVar = 20;
        
        System.out.println(d2.classVar);     // 10 (共享)
        System.out.println(d2.instanceVar);  // 0  (獨立)
    }
}

內存佈局圖解

方法區
+------------------+
| Demo.class       |  ← 包含 classVar = 10
+------------------+

堆
+------------------+   +------------------+
| d1 實例          |   | d2 實例          |
| instanceVar = 20 |   | instanceVar = 0  |
+------------------+   +------------------+

常見誤區與最佳實務

  1. 誤以為 static 修飾對象就不可變:static 只固定引用,對象內部狀態仍可修改。
    正確:static final List LIST = new ArrayList<>(); 引用不可變,但列表內容可變。
  2. 靜態方法中不可直接訪問非靜態成員:因無 this 引用。需先創建實例。
  3. 常量定義:static final 大寫下劃線,編譯期常量內聯,修改需重新編譯調用方。
  4. 單例模式:利用類變量持有唯一實例,配合私有構造器。

進階:static 代碼塊與初始化順序

public class InitOrder {
    static { System.out.println("1. 靜態代碼塊"); }
    static int sv = 10;
    { System.out.println("3. 實例代碼塊"); }
    int iv = 20;
    public InitOrder() { System.out.println("4. 構造器"); }
    
    public static void main(String[] args) {
        System.out.println("2. main 開始");
        new InitOrder();
    }
}
// 輸出順序:1 → 2 → 3 → 4
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved