๐Ÿš€ Nested Class

Java์—๋Š” ์ค‘์ฒฉ ํด๋ž˜์Šค๋ผ๋Š”๊ฒŒ ์กด์žฌํ•œ๋‹ค. C++์—๋„ ์กด์žฌํ•˜์ง€๋งŒ, ๊ทธ ์ข…๋ฅ˜๊ฐ€ ๋” ๋‹ค์–‘ํ•˜๋‹ค. Java Nested Class๋Š” ์˜ค๋ผํด docs๋ฅผ ์ฐธ๊ณ ํ–ˆ๋‹ค. https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

๐Ÿ”ฅ Static Nested Class

๋งŽ์€ ์‚ฌ๋žŒ๋“ค์ด Static Nested Class์— ๋Œ€ํ•ด Static Inner Class๋กœ๋„ ์ง€์นญ์„ ํ•˜๋Š”๋ฐ ๊ณต์‹ ์šฉ์–ด๋Š” Static Nested Class๊ฐ€ ๋งž๋Š” ๊ฒƒ ๊ฐ™๋‹ค.

Terminology: Nested classes are divided into two categories: static and non-static. Nested classes that are declared static are called static nested classes. Non-static nested classes are called inner classes. from https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

Nested Class๋ผ๋Š”๊ฒŒ ๋…ผ๋ฆฌ์ ์œผ๋กœ ๊ฐ™์€ ๋‚ด์šฉ์„ ๊ฐ™์€ Class์— ๋ฌถ๊ธฐ ์œ„ํ•ด ๋‚˜์˜จ ๊ฐœ๋…์ธ๋ฐ, ์ž๋ฐ”๋Š” Static Nested Class๋ผ๋Š” ๊ฐœ๋…์„ ๋งŒ๋“ค์–ด์„œ ์™ธ๋ถ€ ํด๋ž˜์Šค์— ๋Œ€ํ•œ ์ฐธ์กฐ๋ฅผ ๊ฐ€์ง€์ง€ ์•Š๋Š” Nested Class๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค. ์ด๋กœ์จ ์–ป๋Š” ์žฅ์ ์€ GC๊ฐ€ ์ž์› ํšŒ์ˆ˜๋ฅผ ๋” ์‰ฝ๊ฒŒ ํ•  ์ˆ˜ ์žˆ๊ณ (์™ธ๋ถ€ ์ฐธ์กฐ x), ๋˜ Inner Class๋กœ ๋งŒ๋“œ๋Š” ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ ์˜ค๋ฒ„ํ—ค๋“œ๊ฐ€ ๋” ์ž‘์€ ์žฅ์ ์ด ์žˆ๋‹ค.

๐Ÿ”ฅ Inner Class

There are two special kinds of inner classes: local classes and anonymous classes. from https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

docs์—์„œ๋Š” class์•ˆ์— ์„ ์–ธํ•œ class๋Š” Inner Class๋ผ๊ณ  ์„ค๋ช…ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  Inner Class์— 2๊ฐ€์ง€ ํŠน๋ณ„ ์ผ€์ด์Šค๋กœ local class์™€ anonymous class๊ฐ€ ์กด์žฌํ•œ๋‹ค๊ณ  ์„ค๋ช…๋˜์–ด ์žˆ๋‹ค. Class์•ˆ์˜ ๋‹จ์ˆœ Class์˜ ์ด๋ฆ„๋„ ์ •ํ•ด์ค˜์•ผ ํ•  ๊ฒƒ ๊ฐ™์€๋ฐ ์—†์œผ๋‹ˆ.. ํŽธ์˜์ƒ ํด๋ž˜์Šค์•ˆ์˜ ๊ทธ๋ƒฅ ํด๋ž˜์Šค๋Š” Inner Class, ๊ทธ๋ฆฌ๊ณ  ๋‚˜๋จธ์ง€๋„ ์ •ํ™•ํ•˜๊ฒŒ๋Š” Inner Class๊ฐ€ ๋งž์ง€๋งŒ ์‚ฌ์šฉ ์ƒํ™ฉ์— ๋งž๊ฒŒ ์ •ํ™•ํ•œ ๋ช…์นญ์— ๋งž์ถ”์–ด Local Class, Anonymous Class๋ผ๊ณ  ํ•˜๋Š”๊ฒŒ ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค.

Inner Class๋Š” ์„ ์–ธ ๋ฐฉ๋ฒ•๊ณผ ์„ ์–ธ ์œ„์น˜์— ๋”ฐ๋ผ 3๊ฐ€์ง€๋กœ ๋‚˜๋‰œ๋‹ค.

class ์•ˆ์˜ class

class A {
  class B {

  }
}

method ์•ˆ์˜ class

class A {
    void showExample() {
        class B {

        }
    }
}

java๋Š” top-level class๋‚˜ static class๊ฐ€ ์•„๋‹ˆ๋ฉด static member field๋ฅผ inner class์™€ method ์•ˆ์—์„œ ํ—ˆ๋ฝํ•˜์ง€ ์•Š๋Š”๋‹ค. ๊ทธ๋Ÿฐ๋ฐ interface๋Š” inherently staticํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฉ”์†Œ๋“œ์—์„œ interface๋ฅผ ์„ ์–ธํ•˜๊ณ , ๊ตฌํ˜„์ฒด๋ฅผ ์ง์ ‘ ์จ์„œ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์„ ๋•Œ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธฐ๊ฒŒ ๋œ๋‹ค. ๋”ฐ๋ผ์„œ ์ด ๋•Œ Anonymous Class๋ฅผ ์ด์šฉํ•˜๋ฉด, Class์•ˆ์—์„œ Interface๋ฅผ ์„ ์–ธํ•˜๊ณ  method์•ˆ์—์„œ Anonymous Class๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. (Anonymous Class๋Š” expression์ด๋ผ๊ณ  docs์— ์“ฐ์—ฌ์ ธ ์žˆ๋‹ค. new operator๋ฅผ ํ†ตํ•œ ๊ฐ์ฒด ์ƒ์„ฑ ํ‘œํ˜„์‹)

https://stackoverflow.com/questions/41015440/declaring-a-static-variable-in-a-non-static-class-in-java ์ฐธ๊ณ 

class A {
    interface HelloWorld {
        public void greet();
        public void greetSomeone(String someone);
    }
    void showExample() {
        HelloWorld frenchGreeting = new HelloWorld() {
            String name = "tout le monde";
            public void greet() {
                greetSomeone("tout le monde");
            }
            public void greetSomeone(String someone) {
                name = someone;
                System.out.println("Salut " + name);
            }
        };
    }
}