Language/Java

static 블럭의 사용

ilyadelavie 2022. 9. 13. 23:39

static block 이란?


클래스 변수를 초기화시키는 용도의 블럭으로 클래스 변수가 로드될 때 바로 실행된다.

public class CryptoCurrency {
    public static Map<String, String> map = new HashMap<>();

    static {
        map.put("BTC", "Bitcoin");
        map.put("ETH", "Ethereum");
        map.put("ADA", "ADA");
        map.put("POT", "Polkadot");
    }
}

 

프로세스


  1. 클래스 로딩
  2. 클래스 변수 메모리 생성
  3. 선언된 static 블럭 순서대로 실행

 

https://www.geeksforgeeks.org/static-blocks-in-java/

 

Static Blocks in Java - GeeksforGeeks

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

www.geeksforgeeks.org