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

# Inner Class

inner class instance 생성 방법과 주의해야할 점!

* Inner Class Type and Scope

: Same as variable

```java
class Outer {
    class InstanceInner {}
    static class StaticInner {}
    
    void myMethod() {
        class LocalInner {}
    }
}
```

| Inner Class                         | Description                                                                                                                                                                                                                                                                                            |
| ----------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| <p>Instance Class<br>(인스턴스 클래스)</p> | <p>외부 클래스와 멤버 변수 선언 위치에 선언.</p><p>외부 클래스의 인스턴스멤버처럼 다루어진다.</p><p>주로 외부 클래스의 인스턴스멤버들과 관련된 작업에 사용될 목적으로 선언된다.</p><p>static class 사용가능하다.</p>                                                                                                                                                              |
| <p>Static Class<br>(스태틱 클래스)</p>    | <p>외부 클래스의 멤버변수 선언 위치에 선언하며, 외부 클래스의 static 멤버처럼 다루어진다.</p><p>주로 외부 클래스의 static 멤버, 특히 static 메서드에서 사용될 목적으로 선언된다.</p><p>변수와 마찬가지로, static class는 instance class를 사용하지 못한다!</p><p>왜냐하면, instance 클래스는 객체가 생성되고나서 생성되는데, static class는 객체 생성 없이 가능한데 static class에서 사용하는 시점에 객체가 생성됐는지 알 수 없기 때문이다.</p> |
| <p>Local Class<br>(지역 클래스)</p>      | 외부 클래스의 메서드나 초기화블럭 안에 선언하며, 선언된 영역 내부에서만 사용될 수 있다.                                                                                                                                                                                                                                                     |
| <p>Anonymous Class<br>(익명 클래스)</p>  | <p>클래스의 선언과 객체의 생성을 동시에 하는 이름없는 클래스(일회용)</p><p>주로 이벤트 처리에 사용된다.</p><p>like 익명함수</p>                                                                                                                                                                                                                    |

* **Inner Class modifier and scope**

1. 외부 클래스의 private 멤버도 접근가능하다.
2. 외부 클래스의 지역변수는 final이 붙은 변수만 접근가능하다. (JDK 1.8부터 에러가 나진 않지만, 의식적으로 final을 붙여서 사용하도록 하자.)

**If you need static members in inner class, then make it static inner class.**

**Only static inner class can have static members.**

```java
class Ex7_12 {
    class InstanceInner {
        int iv = 100;
        //static int cv = 100;error
        final static int CONST = 100;//여기서 final static은 상수이므로 허용
    }
    //only static inner class can have static members
    static class StaticInner {
        int iv = 200;
        static int cv = 200;
    }
    
    void myMethod() {
        class LocalInner {
            int iv = 300;
            //static int cv = 300;error
            final static int CONST = 300;
        }
    }
}
```

* Creating inner class : inner class is kind of instance member, so only after creating outer class instance, it can be used. Static inner class instance is possible without creating outer class.

```java
class Ex7_15 {
    public static void main(String[] args) {
        //1.Creating outer class instance
        Outer2 oc = new Outer2();
        //2.Creating inner class instance
        Outer2.InstanceInner ii = oc.new InstanceInner();
        
        System.out.println("Outer2.StaticInner.cv : " +Outer2.StaticInner.cv); 
    }
}
```
