# [Effective Java] Item 1. ์ƒ์„ฑ์ž ๋Œ€์‹  ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ๋ฅผ ๊ณ ๋ คํ•˜๋ผ.
Study Repository

[Effective Java] Item 1. ์ƒ์„ฑ์ž ๋Œ€์‹  ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ๋ฅผ ๊ณ ๋ คํ•˜๋ผ.

by rlaehddnd0422

์ž๋ฐ”๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฐœ๋ฐœ์ž๋“ค์—๊ฒŒ ์ง€์ผœ์•ผํ•  ๊ทœ์น™๋“ค์„ 90๊ฐœ์˜ ์•„์ดํ…œ์— ๋‹ด์€ ์ฑ…์ž…๋‹ˆ๋‹ค.

๊ฐ ํ…Œ๋งˆ๋ณ„ ์ฃผ์ œ๋“ค์„ ๋ช…๋ฃŒํ•˜๊ณ  ๋‹จ์ˆœํ•˜๊ฒŒ ํ•œ ๋ฌธ์žฅ์— ๋‹ด์•„ Item์œผ๋กœ ์†Œ๊ฐœํ•˜๋Š” ์ ์—์„œ ๋งˆ์Œ์— ๋“ค์—ˆ๋Š”๋ฐ, ํ•œ ์ฃผ์— 3๊ฐœ ์ •๋„ ์ฝ์–ด๋ณด๋ฉด์„œ ๊ธฐ๋ก์„ ๋‚จ๊ฒจ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 

 

์ฒซ ๋ฒˆ์งธ ์ฑ•ํ„ฐ์—์„œ๋Š” ๊ฐ์ฒด ์ƒ์„ฑ๊ณผ ํŒŒ๊ดด๋ฅผ ๋‹ค๋ฃน๋‹ˆ๋‹ค.

 

ํฌ๊ฒŒ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด์•ผ ํ•  ๋–„ ์ง€์ผœ์•ผํ•  ๊ทœ์น™๊ณผ, ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์ง€ ๋ง์•„์•ผ ํ•  ๋•Œ์™€ ๋งŒ๋“ค์–ด์•ผ ํ•  ๋–„๋ฅผ ๊ตฌ๋ถ„ํ•˜๋Š” ๋ฐฉ๋ฒ•, ์˜ฌ๋ฐ”๋ฅธ ๊ฐ์ฒด ์ƒ์„ฑ ๋ฐฉ๋ฒ•๊ณผ ๋ถˆํ•„์š”ํ•œ ์ƒ์„ฑ์„ ํ”ผํ•˜๋Š” ๋ฐฉ๋ฒ•, ์ œ ๋•Œ ํŒŒ๊ดด๋จ์„ ๋ณด์žฅํ•˜๊ณ  ํŒŒ๊ดด ์ „ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•  ์ •๋ฆฌ ์ž‘์—…์„ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณธ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.


Item 1์˜ main theme

์ƒ์„ฑ์ž ๋Œ€์‹  ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ๋ฅผ ๊ณ ๋ คํ•˜๋ผ.


ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•

  • ์–ด๋–ค ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ ์ผ๋ฐ˜์ ์œผ๋กœ ์•„๋ž˜ 1๋ฒˆ ๋ฐฉ๋ฒ•๊ณผ ๊ฐ™์ด public ์ƒ์„ฑ์ž๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ธ์Šคํ„ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜๊ณค ํ•ฉ๋‹ˆ๋‹ค.

1.  ์ƒ์„ฑ์ž

public class Person {

    private String name;

    /**
     * 1. public ์ƒ์„ฑ์ž
     */
    // name์„ arugment๋กœ ์ฃผ์ž…๋ฐ›์•„ Person ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
    public Person(String name) {
        this.name = name;
    }

    // arugment๋ฅผ ์ฃผ์ž…๋ฐ›์ง€ ์•Š๊ณ  Person ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
    public Person() {
        this.name = "Anonymous";
    }
}

์‚ฌ์‹ค์€ public ์ƒ์„ฑ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• ๋ง๊ณ ๋„ ๋˜ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์ด ํ•˜๋‚˜ ๋” ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ”๋กœ ์ด Item์—์„œ ์ถ”์ฒœํ•˜๋Š” ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์•„๋ž˜ ์ฒ˜๋Ÿผ์š”.

 

2. ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ

public class Person {

    private static Person PERSON = new Person();
    
    /**
     * 2. ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ
     */
    private static Person getInstance() {
        return PERSON;
    }
}

static ์˜์—ญ์— ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค์–ด ๋‘๊ณ , ํ•„์š”ํ•  ๋•Œ ๋งˆ๋‹ค ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

์ด ๋ฐฉ์‹์—๋Š” ์žฅ์ ๊ณผ ๋‹จ์  ๋ชจ๋‘ ์กด์žฌํ•˜๋Š”๋ฐ ๋จผ์ € ์žฅ์ ๋ถ€ํ„ฐ ์•Œ์•„๋ด…์‹œ๋‹ค.


์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ ์‹œ ์žฅ์ 

1. ์ƒ์„ฑ์ž์— ์ด๋ฆ„์„ ๋ถ€์—ฌํ•  ์ˆ˜ ์žˆ๋‹ค.

ํ•œ ํด๋ž˜์Šค์— ๋Œ€ํ•œ ์ƒ์„ฑ์ž๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ ํ•„์š”ํ•˜๋‹ค๋ฉด, ์ƒ์„ฑ์ž์— ๋„˜๊ธฐ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜์™€ ์ƒ์„ฑ์ž ์ž์ฒด๋งŒ์œผ๋กœ๋Š” ๋ฐ˜ํ™˜๋  ๊ฐ์ฒด์˜ ํŠน์„ฑ์„ ์ œ๋Œ€๋กœ ์ดํ•ดํ•˜๊ธฐ์—๋Š” ์–ด๋ ค์›€์ด ์žˆ์Šต๋‹ˆ๋‹ค.

package chapter1.item1;

public class Person {

    private String name;
    private Integer age;
    private String sex;

    public Person(String name) {
        this.name = name;
        this.age = 20;
        this.sex = "๋‚จ์ž";
    }

    public Person(Integer age) {
        this.name = "anonynmous";
        this.age = age;
        this.sex = "๋‚จ์ž";
    }

    public Person(String name, Integer age) {
        this.name = name;
        this.age = age;
        this.sex = "๋‚จ์ž";
    }

    public Person(Integer age, String sex) {
        this.name = "anonynmous";
        this.age = age;
        this.sex = sex;
    }

    public Person(String name, Integer age, String sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
}

 

์ƒ์„ฑ์ž๋ฅผ ์ด๋ ‡๊ฒŒ ๋ฌด๋ถ„๋ณ„ํ•˜๊ฒŒ ๋งŒ๋“ค์–ด ๋†“์œผ๋ฉด ํด๋ผ์ด์–ธํŠธ ์ž…์žฅ์—์„œ๋Š” ๊ฐ ์ƒ์„ฑ์ž๊ฐ€ ์ธ์Šคํ„ด์Šค์˜ attribute์— ์–ด๋–ค ๊ฐ’์„ ๋Œ€์ž…ํ•  ์ง€ ํ˜ผ๋ž€์„ ์ผ์œผํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๋ ‡๊ฒŒ ์ƒ์„ฑ์ž๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ ์กด์žฌํ•˜๊ฒŒ ๋˜๋ฉด, ๊ฐ ์ƒ์„ฑ์ž๊ฐ€ ๋งŒ๋“ค์–ด๋‚ด๋Š” ์ธ์Šคํ„ด์Šค์˜ ํŠน์„ฑ์„ ๋‹จ๋ฒˆ์— ์ดํ•ดํ•˜๊ธฐ ์–ด๋ ต์Šต๋‹ˆ๋‹ค.

 

๋ฐ˜๋ฉด ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ๋Š” ๋ฉ”์†Œ๋“œ ์ž์ฒด์— ์ด๋ฆ„์„ ๋ถ€์—ฌํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ๋ฉ”์†Œ๋“œ ์ž‘๋ช…๋งŒ ์ž˜ ํ•œ๋‹ค๋ฉด ๋ฐ˜ํ™˜๋  ์ธ์Šคํ„ด์Šค์˜ ํŠน์„ฑ์„ ์‰ฝ๊ฒŒ ๋ฌ˜์‚ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

public class Person {

    private String name;
    private Integer age;
    private String sex;

    private static Person createByName(String name) {
        Person person = new Person();
        person.name = name;
        person.age = 20;
        person.sex = "๋‚จ์ž";

        return person;
    }
}

 

์ฆ‰, ํ•˜๋‚˜์˜ createByName ์ด๋ผ๋Š” ์‹œ๊ทธ๋‹ˆ์ฒ˜๋กœ๋Š” ์ƒ์„ฑ์ž๋ฅผ ํ•˜๋‚˜๋งŒ ๋งŒ๋“ค๋„๋ก ํ•จ์œผ๋กœ์จ ๊ฐœ๋ฐœ์ž๋‚˜ ์ฝ”๋“œ๋ฅผ ์ฝ๋Š” ์‚ฌ๋žŒ์œผ๋กœ ํ•˜์—ฌ๊ธˆ ํ˜ผ๋ž€์„ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

2. ํ˜ธ์ถœํ•  ๋•Œ ๋งˆ๋‹ค ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒˆ๋กœ ์ƒ์„ฑํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

public class Human {

    private static final Human DEFAUT_HUMAN = new Human();

    private static Human getInstance() {
        return DEFAUT_HUMAN;
    }
}
public class Main {

    public static void main(String[] args) {
        Human human = Human.getInstance();
    }
}

 

์ด ๋•๋ถ„์— ๋ถˆ๋ณ€ ํด๋ž˜์Šค๋Š” ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฏธ๋ฆฌ ๋งŒ๋“ค์–ด ๋†“๊ฑฐ๋‚˜ ์ƒˆ๋กœ ์ƒ์„ฑํ•œ ์ธ์Šคํ„ด์Šค๋ฅผ ์บ์‹ฑํ•˜์—ฌ ์žฌํ™œ์šฉํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๋ถˆํ•„์š”ํ•œ ๊ฐ์ฒด ์ƒ์„ฑ์„ ํ”ผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

public class Human {

    private static final Human DEFAUT_HUMAN = new Human();
    
    private Human(){}

    public static Human getInstance() {
        return DEFAUT_HUMAN;
    }
}
  • ์ด๋ ‡๊ฒŒ ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๊ธฐ๋ณธ ์ƒ์„ฑ์ž์˜ ์ œ์–ด์ž๋ฅผ private์œผ๋กœ ๋ง‰์•„์คŒ์œผ๋กœ์จ new ํ‚ค์›Œ๋“œ๋ฅผ ํ†ตํ•œ ๊ฐ์ฒด ์ƒ์„ฑ์„ ๋ง‰์•„์ค๋‹ˆ๋‹ค.
@jdk.internal.ValueBased
public final class Boolean implements java.io.Serializable,
                                      Comparable<Boolean>, Constable
{
    /**
     * The {@code Boolean} object corresponding to the primitive
     * value {@code true}.
     */
    public static final Boolean TRUE = new Boolean(true);

    /**
     * The {@code Boolean} object corresponding to the primitive
     * value {@code false}.
     */
    public static final Boolean FALSE = new Boolean(false);
@IntrinsicCandidate
public static Boolean valueOf(boolean b) {
    return (b ? TRUE : FALSE);
}

 

๋Œ€ํ‘œ์ ์ธ ์˜ˆ์‹œ๋กœ, Boolean.valueOf(boolean) ๋ฉ”์†Œ๋“œ๋Š” ์ด๋ ‡๊ฒŒ ๊ฐ์ฒด๋ฅผ ์•„์˜ˆ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ , ๋‚ด๋ถ€์—์„œ static ์˜์—ญ์— ์˜ฌ๋ ค๋‘” ๊ฐ์ฒด๋ฅผ ๊บผ๋‚ด๋‹ค ์จ์„œ ์žฌํ™œ์šฉํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๊ฐ์ฒด๊ฐ€ ์ž์ฃผ ์š”์ฒญ๋˜๋Š” ์ƒํ™ฉ์—์„œ ์ด ํŒจํ„ด์„ ์ ์šฉํ•œ๋‹ค๋ฉด ์„ฑ๋Šฅ์„ ๋Œ์–ด ์˜ฌ๋ ค์ค„ ์ˆ˜ ์žˆ๊ฒ ์Šต๋‹ˆ๋‹ค.

 

์—ฌ๊ธฐ์„œ ๋งํ•˜๋Š” ๋ถˆ๋ณ€ ํด๋ž˜์Šค๋ž€?

1) ๋ง ๊ทธ๋Œ€๋กœ ๋ณ€๊ฒฝ์ด ๋ถˆ๊ฐ€๋Šฅํ•œ ํด๋ž˜์Šค๋กœ, ๋ ˆํผ๋Ÿฐ์Šค ํƒ€์ž…์˜ ๊ฐ์ฒด์ด๋ฉฐ, heap ์˜์—ญ์— ์ƒ์„ฑ๋œ๋‹ค.
2) ๋Œ€ํ‘œ์ ์œผ๋กœ String, Boolean, Integer ๋“ฑ๋“ฑ์ด ์žˆ๋‹ค.

์žฅ์ ์œผ๋กœ๋Š” ๋ฉ€ํ‹ฐ ์Šค๋ ˆ๋“œ ํ™˜๊ฒฝ์—์„œ ๋™๊ธฐํ™” ํ•˜์ง€ ์•Š๊ณ  ๊ณต์œ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— Thread-Safe ํ•˜๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๊ฒ ๋‹ค. (๋ฎ์–ด์“ฐ๋Š” ๋ฌธ์ œ๊ฐ€ ์—†๋‹จ ๋ง์ด๋‹ค.)
๋‹จ์ ์œผ๋กœ๋Š” ๊ฐ์ฒด๊ฐ€ ๊ฐ€์ง€๋Š” ๊ฐ’๋งˆ๋‹ค ์ƒˆ๋กœ์šด ๊ฐ์ฒด๊ฐ€ ํ• ๋‹น๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜๋‚˜ ์ƒ์„ฑ ๋น„์šฉ์— ๋”ฐ๋ฅธ ์„ฑ๋Šฅ ์ €ํ•˜๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜๋„ ์žˆ๊ฒ ๋‹ค.
-> static ์˜์—ญ์€ GC์˜ ๋Œ€์ƒ์ด ๋  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜์— ๋Œ€ํ•ด ์‹ ๊ฒฝ์จ์•ผ ํ•œ๋‹ค.

 

 

์ด์™€ ๋น„์Šทํ•œ ๊ธฐ๋ฒ•์œผ๋กœ๋Š” ํ”Œ๋ผ์ด์›จ์ดํŠธ ํŒจํ„ด (Flyweight pattern)์ด ์žˆ์Šต๋‹ˆ๋‹ค.

  • ๊ณต์œ ๋ฅผ ํ†ตํ•ด ๋‹ค์–‘ํ•œ ๊ฐ์ฒด๋“ค์„ ํšจ๊ณผ์ ์œผ๋กœ ์ง€์›ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ,
    • ๋Œ€ํ‘œ์ ์œผ๋กœ๋Š” String ํƒ€์ž…์˜ ๊ฐ์ฒด๊ฐ€ String Pool์„ ์‚ฌ์šฉํ•ด ์ด๋Ÿฐ ํŒจํ„ด์ด ์ ์šฉ๋˜์–ด ์žˆ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • ์–ด์ฐจํ”ผ ๊ฐ’ ์ž์ฒด๋Š” ๋ถˆ๋ณ€๊ฐ์ฒด๋ผ ๊ฐ™์€ ์ฐธ์กฐ๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๋˜๊ธฐ ๋•Œ๋ฌธ์—, ๋‘ String ์ด ๊ฐ™์€ ๊ฐ’์ด๋ผ๋ฉด ๊ฐ™์€ ์ฐธ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ํ•˜์—ฌ ๋ถˆํ•„์š”ํ•œ ๋™์ผ ๊ฐ์ฒด ์ƒ์„ฑ์„ ๋ง‰๊ณ  ๊ฐ™์€ ์ฐธ์กฐ๋ฅผ ๊ณต์œ ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด์ฃ .

 

3. ๋ฐ˜ํ™˜ ํƒ€์ž…์˜ ํ•˜์œ„ ํƒ€์ž… ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

"๋ฐ˜ํ™˜ํ•  ๊ฐ์ฒด์˜ ํด๋ž˜์Šค๋ฅผ ์ž์œ ๋กญ๊ฒŒ ์„ ํƒํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” ์œ ์—ฐ์„ฑ์„ ์ค๋‹ˆ๋‹ค." 

์ •์  ๋ฉ”์†Œ๋“œ๋กœ๋ถ€ํ„ฐ ์ธํ„ฐํŽ˜์ด์Šค ์ž์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ํ•˜์—ฌ, ๊ตฌํ˜„ ํด๋ž˜์Šค๋ฅผ ๊ณต๊ฐœํ•˜์ง€ ์•Š์œผ๋ฉด์„œ ๊ทธ ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค๋Š” ์˜๋ฏธ๋กœ ์ •๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์‚ฌ์‹ค ์ฝ์–ด๋„ ์˜๋ฏธํŒŒ์•…ํ•˜๊ธฐ๊ฐ€ ๋„ˆ๋ฌด ์–ด๋ ค์›Œ์„œ ์ฝ”๋“œ์˜ˆ์‹œ๋กœ ์ •๋ฆฌํ•˜์‹  ๋ถ„์ด ์žˆ์–ด์„œ ์ฐธ๊ณ ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

public interface Grade {
	String toText();
}

public static Grade of(int score) {
    if(score >= 90) {
        return new A();
    }

    if(score >=80) {
        return new B();
    }

    return new F();
}

public class A implements Grade {

    @Override
    public String toText()
    {
        return "A";
    }
}

public class B implements Grade {

	@Override
    public String toText()
    {
        return "B";
    }
}

public class F implements Grade {
	@Override
	public String toText()
	{
    	return "F";
	}
}

public class Application {
	public static void main(String[] args) {		
        Grade grade = Grade.of(95);
        System.out.println(grade);
    }
}
  • Grade ์ธํ„ฐํŽ˜์ด์Šค์— ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ ํ˜•ํƒœ์˜ of()๋ฉ”์†Œ๋“œ๋กœ ์กฐ๊ฑด์— ๋”ฐ๋ผ ๊ฐ์ฒด๋ฅผ ์„ ํƒํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

๋ฉ”์ธํ•จ์ˆ˜๋ฅผ ์ฃผ๋ชฉํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

๊ตฌํ˜„์ฒด ์ƒ์„ฑ ์‹œ์ ์— ๋‹คํ˜•์„ฑ์„ ํ™œ์šฉํ•˜์—ฌ ์ธํ„ฐํŽ˜์ด์Šค๋กœ ๊ตฌํ˜„์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š”๋ฐ, Grade ์ธํ„ฐํŽ˜์ด์Šค๋Š” ์‹ค์ œ ๊ตฌํ˜„์ฒด๋ฅผ ์—ฐ๊ฒฐํ•ด์ฃผ๋Š” ์—ญํ• ๋กœ ๊ตฌํ˜„์ฒด์˜ ์ƒ์„ฑ ๊ตฌํ˜„ ๋กœ์ง์€ ์ˆจ๊ธฐ๋ฉด์„œ API๋Š” ๋งค์šฐ ๊ฐ€๋ฒผ์›Œ์ง€๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.  

 

* ์ž๋ฐ” 8 ์ด์ „์—๋Š” ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ์ •์  ๋ฉ”์†Œ๋“œ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์—†๋‹ค๋Š” ์ œํ•œ์ด ์žˆ์—ˆ์ง€๋งŒ 8 ์ดํ›„์—๋Š” ์ œํ•œ์ด ํ’€๋ ธ์Œ.

 

4. ์ž…๋ ฅ Argument์— ๋”ฐ๋ผ ๋งค๋ฒˆ ๋‹ค๋ฅธ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

3๋ฒˆ๊ณผ ๋น„์Šทํ•œ ๋งฅ๋ฝ์ž…๋‹ˆ๋‹ค. ์žฅ์  3๋ฒˆ์— ์ž‘์„ฑ๋œ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด score ๊ฐ’์— ๋”ฐ๋ผ ๊ฐ์ฒด๋ฅผ ์„ ํƒ์ ์œผ๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

5. ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ์‹œ์ ์—๋Š” ๋ฐ˜ํ™˜ํ•  ๊ฐ์ฒด์˜ ํด๋ž˜์Šค๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

์ฝ๊ณ  ๋˜ ์ฝ์–ด๋„, ์ž๋ฃŒ๋ฅผ ์ฐพ์•„๋ด๋„ ์ด๊ฑด ๋ฌด์Šจ ๋ง์ธ์ง€ ์ •๋ง ๋ชจ๋ฅด๊ฒ ๋„ค์š”. PASS ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.


์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ ์‹œ ๋‹จ์ 

๋ฌผ๋ก  ์žฅ์ ๋งŒ ์žˆ์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. ๋‹จ์ ์„ ์•Œ์•„๋ด…์‹œ๋‹ค.

 

1. ์ƒ์†์„ ํ•˜๋ ค๋ฉด public์ด๋‚˜ protected ์ƒ์„ฑ์ž๊ฐ€ ํ•„์š”ํ•˜๋ฏ€๋กœ ํ•˜์œ„ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์—†๋‹ค.

public class Person {

    private String name;

    private Person(){}

    private static Person fromName(String name) {
        Person person = new Person();
        person.name = name;
        return person;
    }
}
public class Animal extends Person { // X, Compile Error
}
  • ์œ„์™€ ๊ฐ™์ด ์ •์  ํŒฉํ† ๋ฆฌ ์ƒ์„ฑ์ž ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ๋Š” private์œผ๋กœ ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋ฅผ ๋ง‰์•„๋‘๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ด๊ธฐ ๋•Œ๋ฌธ์—, ์ƒ์†์„ ํ†ตํ•œ ํ™•์žฅ์ด ์–ด๋ ต์Šต๋‹ˆ๋‹ค. 
  • ํ•˜์ง€๋งŒ ์–ด์ฐŒ๋ณด๋ฉด ์ด ์ œ์•ฝ์€ ์ƒ์†๋ณด๋‹ค ์ปดํฌ์ง€์…˜์„ ์‚ฌ์šฉํ•˜๋„๋ก ์œ ๋„ํ•จ๊ณผ, ๋™์‹œ์— ๋ถˆ๋ณ€ํƒ€์ž…์œผ๋กœ ๋งŒ๋“ค๋ ค๋ฉด ์ด ์ œ์•ฝ์„ ์ง€์ผœ์•ผ ํ•œ๋‹ค๋Š” ์ ์—์„œ ์žฅ์ ์œผ๋กœ ๋ณผ ์ˆ˜๋„ ์žˆ๊ฒ ์Šต๋‹ˆ๋‹ค. 

* ์ถ”ํ›„ Item 17, Item 18์—์„œ ์ด ๋‚ด์šฉ์ด ์–ธ๊ธ‰

 

2. ํ”„๋กœ๊ทธ๋ž˜๋จธ ์ž…์žฅ์—์„œ๋Š” ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ๋ฅผ ์ฐพ๊ธฐ๊ฐ€ ์–ด๋ ต๋‹ค. 

์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ๋Š” ์ผ๋ฐ˜์ ์ธ ์ƒ์„ฑ์ž์ฒ˜๋Ÿผ API ์„ค๋ช…์— ๋ช…ํ™•ํžˆ ๋“œ๋Ÿฌ๋‚˜์ง€ ์•Š์•„, ์‚ฌ์šฉ์ž ์ž…์žฅ์—์„œ๋Š” ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ ๋ฐฉ์‹ ํด๋ž˜์Šค๋ฅผ ์ธ์Šคํ„ด์Šคํ™”ํ•  ๋ฐฉ๋ฒ•์„ ์ง์ ‘ ์•Œ์•„๋‚ด์•ผ ํ•œ๋‹ค๋Š” ์–ด๋ ค์›€์ด ์žˆ๋‹ค๋Š” ์ ์„ ๋งํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. 


์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ์˜ ๋„ค์ด๋ฐ ์ปจ๋ฒค์…˜

keyword ์„ค๋ช… ์˜ˆ์‹œ
from ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ํ•˜๋‚˜ ๋ฐ›์•„์„œ ํ•ด๋‹น ํƒ€์ž…์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ˜•๋ณ€ํ™˜ ๋ฉ”์†Œ๋“œ People p = People.from(name);
of ์—ฌ๋Ÿฌ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋ฐ›์•„ ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜ People p = People.of(name, age, sex);
valueOf from๊ณผ of์˜ ๋” ์ž์„ธํ•œ ๋ฒ„์ „ BigInteger prime =
BigInteger.valueOf(Integer.MAX_VALUE);
instance, getInstance ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ช…์‹œํ•œ ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜์ง€๋งŒ, ๊ฐ™์€  ์ธ์Šคํ„ด์Šค์ž„์„ ๋ณด์žฅํ•˜์ง€๋Š” ์•Š์Œ. StackWalker luke = StackWalker.getInstance(options);
create, newInstance instance, getInstance์™€ ๊ฐ™์ง€๋งŒ ๋งค๋ฒˆ ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•ด ๋ฐ˜ํ™˜ํ•จ์„ ๋ณด์žฅ. Object newArray = Array.newInstance(classObject, arrayLen);
getType getInstance์™€ ๊ฐ™์œผ๋‚˜ ์ƒ์„ฑํ•  ํด๋ž˜์Šค๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ ํด๋ž˜์Šค์— ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ๋ฅผ ์ •์˜ํ•  ๋•Œ ์‚ฌ์šฉ FileStore fs = Files.getFileStoere(path);
newType newInstance์™€ ๊ฐ™์œผ๋‚˜ ์ƒ์„ฑํ•  ํด๋ž˜์Šค๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ ํด๋ž˜์Šค์— ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ๋ฅผ ์ •์˜ํ•  ๋•Œ ์‚ฌ์šฉ BufferedReader br = Files.newBufferedReader(path);
type getType, newType์˜ ๊ฐ„๊ฒฐํ•œ ๋ฒ„์ „ List<Compaint> litany = Collections.list(legacyLitany);

ํ•ต์‹ฌ ์ •๋ฆฌ

์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ, public ์ƒ์„ฑ์ž๋Š” ๊ฐ์ž์˜ ์“ฐ์ž„์ƒˆ๊ฐ€ ์žˆ๋‹ค. ์ƒ๋Œ€์ ์ธ ์žฅ๋‹จ์ ์„ ์ž˜ ์ดํ•ดํ•˜๊ณ  ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ํ•˜์ง€๋งŒ ๋Œ€์ฒด๋กœ ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์œ ๋ฆฌํ•œ ์ ์ด ๋งŽ๊ธฐ ๋•Œ๋ฌธ์— ๋ฌด์ž‘์ • public ์ƒ์„ฑ์ž๋ฅผ ์“ฐ๋Š” ์Šต๊ด€์ด ์žˆ๋‹ค๋ฉด ๊ณ ์น˜์ž.

 

์ด๋ ‡๊ฒŒ Item1 ์„ ํ•œ ๋ฒˆ ์ •๋ฆฌํ•ด๋ดค๋Š”๋ฐ์š”.

๋‚ด์šฉ์ด ์ƒ๊ฐ๋ณด๋‹ค ๊นŠ๊ณ  ์„ค๋ช…์ด ๋‚œํ•ดํ•  ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, ์•„๋ฌด๋ž˜๋„ ๋ฒˆ์—ญ๋ณธ์ด๋‹ค ๋ณด๋‹ˆ ํ™• ์™€๋‹ฟ๊ฒŒ ์ดํ•ดํ•˜๋Š”๋ฐ๋Š” ์–ด๋ ค์›€์ด ์žˆ๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์„œ์ˆ ๋ฐฉ์‹์ด ๋ƒ…๋‹ค ๋ณธ๋ก ์„ ๋˜์ ธ๋ฒ„๋ฆฌ๊ณ , ์„ค๋ช…ํ•˜๋Š” ๋ฐฉ์‹์ด๋ผ ์ฑ…์˜ ํ…์ŠคํŠธ๋กœ๋งŒ ์˜จ์ „ํžˆ ์ดํ•ดํ•จ์—๋„ ์–ด๋ ค์›€์ด ์žˆ์–ด, ๋Š๋ฆฌ๋”๋ผ๋„ ์ž๋ฃŒ๋ฅผ ์ฐพ์•„๊ฐ€๋ฉด์„œ ๊ผผ๊ผผํ•˜๊ฒŒ ๊ณต๋ถ€ํ•ด์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค.


<์ฐธ๊ณ ์ž๋ฃŒ>

 

Java - ๋ถˆ๋ณ€๊ฐ์ฒด(Immutable Object)์— ๋Œ€ํ•ด

์ข‹์€ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ธฐ ์œ„ํ•œ ๊ณต๋ถ€๋ฅผ ํ•˜๋‹ค๋ณด๋ฉด ๋ถˆ๋ณ€๊ฐ์ฒด์— ๋Œ€ํ•ด ๋“ฃ๊ฒŒ ๋˜๋Š”๋ฐ, ์˜ค๋Š˜์€ ์ด ๋ถˆ๋ณ€๊ฐ์ฒด๋ž€ ๋ฌด์—‡์ธ์ง€์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

min103ju.github.io

 

[Java] String ๊ฐ์ฒด์˜ ๋ถˆ๋ณ€์„ฑ(Immutablity)์— ๋Œ€ํ•ด

Java์—์„œ String์€ ๋ถˆ๋ณ€ ๊ฐ์ฒด๋ผ๊ณ  ๋ถˆ๋ฆฝ๋‹ˆ๋‹ค. ๋ถˆ๋ณ€ ๊ฐ์ฒด๋ž€, ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋œ ์ดํ›„ ์ƒํƒœ๊ฐ€ ๋ณ€ํ•˜์ง€ ์•Š๊ณ  ๊ณ„์† ์œ ์ง€๋˜๋Š” ๊ฐ์ฒด๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰ String ์ธ์Šคํ„ด์Šค๋Š” ํ•œ ๋ฒˆ ์ƒ์„ฑ๋˜๋ฉด ๊ทธ ๊ฐ’์„ ์ฝ๊ธฐ๋งŒ ํ•  ์ˆ˜ ์žˆ๊ณ ,

rlaehddnd0422.tistory.com

 

[JAVA] ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜(Memory Leak)์™€ GC ์„ฑ๋Šฅ ํŠœ๋‹

๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜(memory leak)๋ž€? CS ์˜๋ฏธ๋กœ ์‚ดํŽด๋ณผ ๋•Œ, ์ปดํ“จํ„ฐ ํ”„๋กœ๊ทธ๋žจ์ด ํ•„์š”ํ•˜์ง€ ์•Š์€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๊ณ„์† ์ ์œ ํ•˜๊ณ  ์žˆ๋Š” ํ˜„์ƒ์ด๋‹ค. ํ• ๋‹น๋œ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•œ ๋‹ค์Œ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด ๋ˆ„์ ๋˜๋ฉด ๋ฉ”๋ชจ๋ฆฌ๊ฐ€

junghyungil.tistory.com

 

[์ดํŽ™ํ‹ฐ๋ธŒ ์ž๋ฐ”] ๊ฐ์ฒด์˜ ์ƒ์„ฑ๊ณผ ํŒŒ๊ดด Item1 - ์ƒ์„ฑ์ž ๋Œ€์‹  ์ •์  ํŒฉํ„ฐ๋ฆฌ ๋ฉ”์„œ๋“œ๋ฅผ ๊ณ ๋ คํ•˜๋ผ

์ดํŽ™ํ‹ฐ๋ธŒ ์ž๋ฐ” 2์žฅ. ๊ฐ์ฒด์˜ ์ƒ์„ฑ๊ณผ ํŒŒ๊ดด Item1 - "์ƒ์„ฑ์ž ๋Œ€์‹  ์ •์  ํŒฉํ„ฐ๋ฆฌ ๋ฉ”์„œ๋“œ๋ฅผ ๊ณ ๋ คํ•˜๋ผ"

velog.io

 

๋ธ”๋กœ๊ทธ์˜ ์ •๋ณด

Study Repository

rlaehddnd0422

ํ™œ๋™ํ•˜๊ธฐ