# 靜態成員 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的值，印出來。 ![](/files/-MLRnl0qr5ruVpW-bOYo)

用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的成員放入記憶體中，之後才開始執行程式。

![](/files/-MLRnl0rYW-wSOhlb9qU)

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

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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://yubin551.gitbook.io/java-note/basic_java_programming/reservedwordstatic.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
