> For the complete documentation index, see [llms.txt](https://yubin551.gitbook.io/java-note/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://yubin551.gitbook.io/java-note/basic_java_programming/reservedwordstatic.md).

# 靜態成員 static

> 了解關鍵字static的基本意義。

## 靜態成員修飾子 static

static 是一個關鍵字，是用來修飾成員(member，類別的屬性、方法或子類別)，使其成為靜態成員。

靜態的意思是，在程式載入記憶體的時候，跟著程式一起在記憶體中佔有空間，而不是主程式開始執行後才跟記憶體要空間。

舉個例子：

這是沒用static修飾的一般成員

```java
class Test{
  public static void main(String[] args){
    int value = 10; 
    System.out.println(value);
  }//end of main(String[])
}//end of class Test
```

執行結果：

```java
10
```

JVM載入程式後，開始執行。int value = 10; 開始跟記憶體要一塊int大小的空間，放入數值10。System.out.println(value);讀取value的值，印出來。 ![](https://1201963393-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MLRnhT9aTkN9HvaBP28%2Fsync%2F1f475763ebd4df069cf7679bfa2f58dd236ca020.jpg?generation=1604653623782572\&alt=media)

用static修飾：

```java
class Test{
  static int value = 10;
  public static void main(String[] args){
    System.out.println(value);
  }// end of main(String[])
}// end of class Test
```

執行結果：

```java
10
```

JVM載入程式後，跟著程式原始碼將static的成員放入記憶體中，之後才開始執行程式。

![](https://1201963393-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MLRnhT9aTkN9HvaBP28%2Fsync%2F7216faa604e0e29bd74638976b3ea0c52f106a0a.jpg?generation=1604653623671031\&alt=media)

比較有static跟沒有static的差異，一個決定性的不同是**載入記憶體的時機**。

因為一開始就存在於記憶體之中，所以稱為**靜態**(static)。
