• 코드:
​x
 
1
class InitBlock {
2
    static int classVar = 10;                   // 클래스 변수의 명시적 초기화
3
    int instanceVar = 10;                       // 인스턴스 변수의 명시적 초기화
4
    static { classVar = 20; }                   // 클래스 초기화 블록을 이용한 초기화
5
    { instanceVar = 20; }                       // 인스턴스 초기화 블록을 이용한 초기화
6
    InitBlock() { instanceVar = 30; }           // 생성자를 이용한 초기화
7
}
8
​
9
public class prog {
10
    public static void main(String[] args) {
11
        System.out.println(InitBlock.classVar);
12
        InitBlock myInit = new InitBlock();
13
        System.out.println(myInit.instanceVar);
14
    }
15
}
표준입력 & 실행옵션