header-img
Info :
728x90

static์ด๋ž€?

(์ •์ ์ธ, ์›€์ง์ž„์ด ์—†๋Š”)

 

static ์„ ์‚ฌ์šฉํ•˜์—ฌ ์„ ์–ธํ•˜๋ฉด ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ jvm์˜ static ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ผ๊ฐ€๊ฒŒ ๋œ๋‹ค.

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

 

static์„ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ์˜๋ฏธ๋Š” ํ•ด๋‹น ๊ฐ์ฒด๋ฅผ ๊ณต์œ ํ•œ๋‹ค๋Š” ์˜๋ฏธ.

 

๋™์ผํ•œ ๊ฐ์ฒด๋กœ ๊ฐ’๋งŒ ๋ฐ”๊พธ์–ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์Œ๊ณผ ๋™์‹œ์—

๋™์ผํ•œ ๊ฐ์ฒด๋ฅผ ๊ฐ’๋งŒ ๋ฐ”๊พธ์–ด ์‚ฌ์šฉํ•œ๋‹ค๋Š” ์ ์—์„œ ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ์‹ ์ค‘ํ•˜๊ฒŒ ๋ฐ”๊พธ์–ด์•ผ ํ•œ๋‹ค๋Š” ์ƒ๊ฐ๋„ ํ•ด์•ผํ•œ๋‹ค.

 

 

final์ด๋ž€?

๋ถˆ๋ณ€๊ณผ ๊ด€๋ จ๋œ ์†์„ฑ์ด ์•„๋‹ˆ๋ผ ์žฌํ• ๋‹น ๋ฐฉ์ง€์™€ ๊ด€๋ จ๋œ ์†์„ฑ.

final์„ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด ์žฌํ• ๋‹น์ด ๋ฐฉ์ง€๋˜๋ฏ€๋กœ, ์ƒ์†์ด๋‚˜ ๊ฐ’ ์žฌ ์ดˆ๊ธฐํ™” ๋“ฑ์ด ๋ถˆ๊ฐ€๋Šฅํ•ด์ง„๋‹ค.

public class MutableTest {

    private final Map<String, Object> mutableMap = new HashMap<>();

    public void testFinal() {
        // ์žฌํ• ๋‹น ์•ˆ๋จ
        // mutableMap = new HashMap<>();

        mutableMap.put("choco", "M&M");
        mutableMap.put("jelly", "haribo");

        System.out.println(mutableMap.toString());
    }

    public static void main(String[] args) {
        MutableTest mutableTest = new MutableTest();
        mutableTest.testFinal();
    }
}

 

์žฌํ• ๋‹น์€ ์•ˆ๋˜์ง€๋งŒ ๊ฐ’์˜ ๋ณ€๊ฒฝ์€ ๊ฐ€๋Šฅํ•ด์ง„๋‹ค.

 

๋ณดํ†ต Collectionsํƒ€์ž…(Map, List, Set)์—์„œ ๋งŽ์ด ์‚ฌ์šฉ๋˜๊ณ ,
๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ํƒ€์ž…์ธ int, boolean, char ๋“ฑ๋“ฑ์—์„œ๋Š” ์žฌํ• ๋‹น์ด ์•ˆ๋˜๋‹ˆ ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ๋ฐฉ๋ฒ•์ด ์—†์Œ.

(logger ๊ฐ™์€ ๊ฐ’์ด ๋ณ€ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๋„ final๋กœ ํ• ๋‹นํ•˜๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค.)

 

 

์š”์•ฝ: private static final๊ณผ private final์˜ ์ฐจ์ด

 

private static final

์žฌํ• ๋‹นํ•˜์ง€ ๋ชปํ•˜๋ฉฐ, ๋ฉ”๋ชจ๋ฆฌ์— ํ•œ ๋ฒˆ ์˜ฌ๋ผ๊ฐ€๋ฉด ๊ฐ™์€ ๊ฐ’์„ ํด๋ž˜์Šค ๋‚ด๋ถ€์˜ ์ „์ฒด ํ•„๋“œ, ๋ฉ”์„œ๋“œ์—์„œ ๊ณต์œ ํ•จ.

 

private final

์žฌํ• ๋‹นํ•˜์ง€ ๋ชปํ•˜๋ฉฐ, ํ•ด๋‹น ํ•„๋“œ, ๋ฉ”์„œ๋“œ ๋ณ„๋กœ ํ˜ธ์ถœํ•  ๋•Œ๋งˆ๋‹ค ์ƒˆ๋กญ๊ฒŒ ๊ฐ’์„ ํ• ๋‹นํ•จ.(์ธ์Šคํ„ด์Šคํ™”)

 

728x90
๋”๋ณด๊ธฐ
BACKEND/SpringBoot