[์ดํŽ™ํ‹ฐ๋ธŒ ์ž๋ฐ”] Item08 ์™„๋ฒฝ๊ณต๋žต

2023. 1. 25. 11:05ใ†JAVA/Effective JAVA

728x90
item08. finalizer์™€ cleaner ์‚ฌ์šฉ์„ ํ”ผํ•˜๋ผ.  

" p45. ์ •์ ์ด ์•„๋‹Œ ์ค‘์ฒฉ ํด๋ž˜์Šค๋Š” ์ž๋™์œผ๋กœ ๋ฐ”๊นฅ ๊ฐ์ฒด์˜ ์ฐธ์กฐ๋ฅผ ๊ฐ–๋Š”๋‹ค. & ๋žŒ๋‹ค ์—ญ์‹œ ๋ฐ”๊นฅ ๊ฐ์ฒด์˜ ์ฐธ์กฐ๋ฅผ ๊ฐ–๊ธฐ ์‰ฝ๋‹ค. "

 

 

https://hyejin.tistory.com/1011

 

[์ดํŽ™ํ‹ฐ๋ธŒ ์ž๋ฐ”] Item8. finalizer์™€ cleaner ์‚ฌ์šฉ์„ ํ”ผํ•˜๋ผ.

item07. ๋‹ค ์“ด ๊ฐ์ฒด ์ฐธ์กฐ๋ฅผ ํ•ด์ œํ•˜๋ผ. ์ดํŽ™ํ‹ฐ๋ธŒ ์ž๋ฐ” part 1์—์„œ๋Š” ๊ฐ์ฒด์˜ ์ƒ์„ฑ๊ณผ ์†Œ๋ฉธ์— ๋Œ€ํ•ด์„œ ์–˜๊ธฐํ•˜๊ณ  ์žˆ๋Š”๋ฐ ์ง€๊ธˆ์€ ์ €๋ฒˆ item ๋ถ€ํ„ฐ๋Š” ๊ฐ์ฒด ์†Œ๋ฉธ์— ๋Œ€ํ•ด์„œ ์ด์•ผ๊ธฐ ํ•˜๊ณ  ์žˆ๋‹ค. ์šฐ์„  ์ž๋ฐ”์—์„œ๋Š” ๋‘

hyejin.tistory.com

์—ฌ๊ธฐ์„œ State ์ธ์Šคํ„ด์Šค๋Š” Room ์ธ์Šคํ„ด์Šค๋ฅผ ์ ˆ๋Œ€๋กœ ์ฐธ์กฐํ•˜๋ฉด ์•ˆ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ •์  ์ค‘์ฒฉ ํด๋ž˜์Šค๋กœ ์ž‘์„ฑํ–ˆ๋‹ค. 

Room ์ธ์Šคํ„ด์Šค๋ฅผ ์ฐธ์กฐํ•  ๊ฒฝ์šฐ์—” ์ˆœํ™˜์ฐธ์กฐ๊ฐ€ ์ƒ๊ฒจ์„œ ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰ํ„ฐ๊ฐ€ Room ์ธ์Šคํ„ด์Šค๋ฅผ ํšŒ์ˆ˜ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. 

 

์ค‘์ฒฉ ํด๋ž˜์Šค๋กœ ๋งŒ๋“ค ๊ฒฝ์šฐ์— static์œผ๋กœ ๋งŒ๋“ค์ง€ ์•Š์œผ๋ฉด ์ž๋™์œผ๋กœ ๋ฐ”๊นฅ ๊ฐ์ฒด์˜ ์ฐธ์กฐ๋ฅผ ๊ฐ–๊ธฐ ๋•Œ๋ฌธ์— ์ˆœํ™˜์ฐธ์กฐ๊ฐ€ ์ƒ๊ฒจ์„œ Room ์ธ์Šคํ„ด์Šค๋ฅผ ํšŒ์ˆ˜ํ•ด ๊ฐ€์ง€ ์•Š๋Š”๋‹ค. 

 

 

package chapter01.item08.outerclass;

import java.lang.reflect.Field;

/**
 * item08. finalizer ์™€ cleaner ์‚ฌ์šฉ์„ ํ”ผํ•˜๋ผ.
 * ์™„๋ฒฝ๊ณต๋žต - ์ •์ ์ด ์•„๋‹Œ ์ค‘์ฒฉ ํด๋ž˜์Šค๋Š” ์ž๋™์œผ๋กœ ๋ฐ”๊นฅ ๊ฐ์ฒด์˜ ์ฐธ์กฐ๋ฅผ ๊ฐ–๋Š”๋‹ค.
 */
public class OuterClass {
    private void hi() {

    }
    class InnerClass {
        public void hello() {
            OuterClass.this.hi(); // ์ •์ ์ด ์•„๋‹Œ ์ค‘์ฒฉ ํด๋ž˜์Šค๋Š” ์ž๋™์œผ๋กœ ๋ฐ”๊นฅ ๊ฐ์ฒด์˜ ์ฐธ์กฐ๋ฅผ ๊ฐ–๋Š”๋‹ค.
        }
    }

    public static void main(String[] args) {
        OuterClass outerClass = new OuterClass();
        InnerClass innerClass = outerClass.new InnerClass();
        System.out.println("innerClass = " + innerClass);

        outerClass.printField();
    }

    private void printField() {
        Field[] declaredFields = InnerClass.class.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            System.out.println("field type : " + declaredField.getType());
            System.out.println("field name : " + declaredField.getName());
        }
    }
}
package chapter01.item08.outerclass;

import java.lang.reflect.Field;

/**
 * item08. finalizer ์™€ cleaner ์‚ฌ์šฉ์„ ํ”ผํ•˜๋ผ.
 * ์™„๋ฒฝ๊ณต๋žต - ์ •์ ์ด ์•„๋‹Œ ์ค‘์ฒฉ ํด๋ž˜์Šค๋Š” ์ž๋™์œผ๋กœ ๋ฐ”๊นฅ ๊ฐ์ฒด์˜ ์ฐธ์กฐ๋ฅผ ๊ฐ–๋Š”๋‹ค.
 */
public class OuterClass {

    class InnerClass {
    }

    public static void main(String[] args) {
        OuterClass outerClass = new OuterClass();
        InnerClass innerClass = outerClass.new InnerClass();
        System.out.println("innerClass = " + innerClass);

        outerClass.printField();
    }

    private void printField() {
        Field[] declaredFields = InnerClass.class.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            System.out.println("field type : " + declaredField.getType());
            System.out.println("field name : " + declaredField.getName());
        }
    }
}

-> ๋ฐ”๊นฅ ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋”๋ผ๋„ ๋ฐ”๊นฅ๊ฐ์ฒด์˜ ์ฐธ์กฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. 

์ด๋ ‡๊ฒŒ ์ •์  ์ค‘์ฒฉ ํด๋ž˜์Šค๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ์—๋Š” ๋ฐ”๊นฅ ํด๋ž˜์Šค์˜ ์ฐธ์กฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

 

๊ทธ๋ฆฌ๊ณ  ๋น„์Šทํ•˜๊ฒŒ ๋žŒ๋‹ค ์—ญ์‹œ ๋ฐ”๊นฅ ๊ฐ์ฒด์˜ ์ฐธ์กฐ๋ฅผ ๊ฐ–๊ธฐ ์‰ฌ์šฐ๋ฏ€๋กœ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. 

package chapter01.item08.outerclass;

import java.lang.reflect.Field;

/**
 * item08. finalizer ์™€ cleaner ์‚ฌ์šฉ์„ ํ”ผํ•˜๋ผ.
 * ์™„๋ฒฝ๊ณต๋žต - ๋žŒ๋‹ค ์—ญ์‹œ ๋ฐ”๊นฅ ๊ฐ์ฒด์˜ ์ฐธ์กฐ๋ฅผ ๊ฐ–๊ธฐ ์‰ฝ๋‹ค.
 */
public class LambdaExample {
    private int value = 10;

    private Runnable instanceLambda = () -> {
        System.out.println(value); // static์ด ์•„๋‹ˆ๋ฉด ๋ฐ”๊นฅ ๊ฐ์ฒด์˜ ์ฐธ์กธ๋ฅผ ๊ฐ–๊ธฐ ์‰ฝ๋‹ค.
    };

    public static void main(String[] args) {
        LambdaExample example = new LambdaExample();
        Field[] declaredFields = example.instanceLambda.getClass().getDeclaredFields();
        for (Field declaredField : declaredFields) {
            System.out.println("field type : " + declaredField.getType());
            System.out.println("field name : " + declaredField.getName());
        }
    }
}

 

public class LambdaExample {
    private static int value = 10;

    private static Runnable instanceLambda = () -> {
        System.out.println(value); // static์ด ์•„๋‹ˆ๋ฉด ๋ฐ”๊นฅ ๊ฐ์ฒด์˜ ์ฐธ์กฐ๋ฅผ ๊ฐ–๊ธฐ ์‰ฝ๋‹ค.
    };

    public static void main(String[] args) {
        LambdaExample example = new LambdaExample();
        Field[] declaredFields = example.instanceLambda.getClass().getDeclaredFields();
        for (Field declaredField : declaredFields) {
            System.out.println("field type : " + declaredField.getType());
            System.out.println("field name : " + declaredField.getName());
        }
    }
}

๊ทผ๋ฐ static์œผ๋กœ ํ•˜๋ฉด ๋ฐ”๊นฅ ๊ฐ์ฒด์˜ ์ฐธ์กฐ๋ฅผ ํ•˜์ง€ ์•Š๋Š”๋‹ค. 

 

 

 

๐Ÿฅ ๋”ฐ๋ผ์„œ ์ •์ ์ด ์•„๋‹Œ ์ค‘์ฒฉ ํด๋ž˜์Šค๋Š” ์ž๋™์œผ๋กœ ๋ฐ”๊นฅ ๊ฐ์ฒด์˜ ์ฐธ์กฐ๋ฅผ ๊ฐ–๊ธฐ ๋•Œ๋ฌธ์— ์ •์  ์ค‘์ฒฉ ํด๋ž˜์Šค๋กœ ๋งŒ๋“ค์–ด ์ค˜์•ผ ํ•˜๊ณ , ๋น„์Šทํ•˜๊ฒŒ ๋žŒ๋‹ค ์—ญ์‹œ ๋ฐ”๊นฅ ๊ฐ์ฒด์˜ ์ฐธ์กฐ๋ฅผ ๊ฐ–๊ธฐ ์‰ฝ๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. 

(๊ทธ๋ฆฌ๊ณ  ๋ฌผ๋ก  ์ค‘์ฒฉ ํด๋ž˜์Šค๊ฐ€ ์•„๋‹Œ ์•„์˜ˆ ๋”ฐ๋กœ ํด๋ž˜์Šค๋ฅผ ๋ถ„๋ฆฌํ•˜๊ฒŒ ๋˜๋ฉด ์ƒ๊ด€์—†๋‹ค.)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

728x90