> For the complete documentation index, see [llms.txt](https://incheol-jung.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://incheol-jung.gitbook.io/docs/study/effective-java/undefined-2/2020-03-20-effective-25item.md).

# 아이템25 톱레벨 클래스는 한 파일에 하나만 담으라

소스 파일 하나에 `톱레벨` 클래스를 여러 개 선언 하더라도 자바 `컴파일러`는 불평하지 않는다. 하지만 이렇게 되면 한 클래스를 여러 가지로 정의할 수 있으며, 그중 어느 것을 사용할지는 어느 소스 파일을 먼저 `컴파일`하냐에 따라 달라지기 때문에 **사용을 권장 하지는 않는다.**

```java
public class Main {
    public static void main(String[] args) {
    System.out.println(Utensil.NAME + Dessert.NAME);
    }
}
```

Utensil.java

```java
class Utensil {
    static final String NAME = "pan";
}

class Dessert {
    static final String NAME = "cake";
}
```

Dessert.java

```java
class Utensil {
    static final String NAME = "pot";
}

class Dessert {
    static final String NAME = "pie";
}
```

운 좋게 "**javac Main.java Dessert.java**" 명령으로 `컴파일` 한다면 컴파일 오류가 나고 `Utensil`과 `Dessert` 클래스를 중복 정의 했다고 알려줄 것이다. 컴파일러는 가장 먼저 `Main.java`를 컴파일하고, 그 안에서 (`Dessert` 참조보다 먼저 나오는) `Utensil` 참조를 만나면 `Utensil.java` 파일을 살펴 `Utensil`과 `Dessert`를 모두 찾아낼 것이다. 그런 다음 컴파일러가 두 번째 명령줄 인수로 넘어온 `Dessert.java`를 처리하려 할 때 같은 클래스의 정의가 이미 있음을 알게 된다.

굳이 여러 톱레벨 클래스를 한 파일에 담고 싶다면 `정적 멤버 클래스`를 사용하는 방법을 고민해볼 수 있다. 읽기 좋고 `private`으로 선언하면 `접근 범위`도 최소로 관리할 수 있기 때문이다.

## 정리

소스 파일 하나에는 반드시 `톱레벨 클래스`(혹은 `톱레벨 인터페이스`)를 하나만 담자. 이 규칙만 따른다면 컴파일러가 한 클래스에 대한 정의를 여러 개 만들어 내는 일은 사라진다.


---

# 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://incheol-jung.gitbook.io/docs/study/effective-java/undefined-2/2020-03-20-effective-25item.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.
