> For the complete documentation index, see [llms.txt](https://yenkang-2.gitbook.io/java-learning/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://yenkang-2.gitbook.io/java-learning/week5-inner-class-and-anonymous-class.md).

# week5 Inner Class and Anonymous Class

## 5.5內部類與匿名類

> * 內部類
> * 嵌套類
> * 匿名類

### 內部類（Inner class）:

* 內部類不與外部類同名
* 外部對象名.new 內部類
* 外類類名.this.字段及方法
* 代碼:TestInnerClass.java與TestInnerThis.java
* 內部類的修飾符：外部只能用public與默認
  * Static 修飾符：
    * 實際是外部class
    * new前面不用對象實例
    * 不能訪問**非**static
    * 代碼：TestInnerStatic.java
* 局部類：
  * 在方法建立的類
  * Code:TestInnerMethod.java
  * 不能用：public, private, protected, static等方式
  * 不能訪問方法的local variable，除非是final

### 匿名類（anonymous class）:

* 1次性使用
* new 類名或接口名，不用extends及implement
* 不能定義構造方法
* Code：SortTest.java

### 總結：

此章最主要提及內部類（包括嵌套類、局部類），還有匿名類的特性，然後4個code講解（需再次demo＋抄寫一遍），此印象深刻的點就是，語法正確，但是邏輯有問題的問題，需要再刻意演練一遍。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://yenkang-2.gitbook.io/java-learning/week5-inner-class-and-anonymous-class.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
