제리의 배움 기록
[Java] 생성자와 초기화 블럭 실행 순서 본문
생성자와 초기화 블럭 실행 순서
실행순서
클래스 초기화 블럭 -> 인스턴스 초기화 블록 -> 생성자
- 클래스 초기화 블럭 : 클래스 로더가 클래스를 메모리에 올릴때 최초 한번만 실행
- 인스턴스 초기화 블록, 생성자 : 인스턴스 생성마다 실행
public class TestInstant {
public static void main(String[] args){
TestInstant testInstant =new TestInstant("first");
TestInstant testInstant2 =new TestInstant("second");
TestInstant testInstant3 =new TestInstant("third");
}
// 생성자
public TestInstant(String name){
System.out.println("construct : "+name);
}
// 인스턴스 초기화 블록
{
System.out.println("instant init");
}
// 클래스 초기화 블록
static {
System.out.println("class init");
}
}
출력
class init // static 블록. 클래스 로드
instant init // 인스턴스 초기화
construct : first // 생성자
instant init // 인스턴스 초기화
construct : second // 생성자
instant init // 인스턴스 초기화 블록
construct : third // 생성자
'자바' 카테고리의 다른 글
[Java] 2차원 배열은 어떻게 메모리에 저장될까? (0) | 2021.11.12 |
---|---|
[Java] Reference Type : 참조유형 (0) | 2021.11.12 |
[Java] float와 double 연산이 제대로 되지 않는 이유 (0) | 2021.11.12 |
[Java] 메서드 시그니처 (0) | 2021.11.12 |
[Java] ArrayList의 toArray() (0) | 2021.11.12 |
Comments