티스토리 뷰
초기화 코드를 넣는 Initialization Blocks
Static Initialization Block
자바 코드들을 읽다 보면 다음과 같은 코드가 쉽게 눈에 띈다
class Whatever { public static final int myInt; static { myInt = 4444; } }
바로 static 초기화 블럭이다
클래스가 생성되는 시점에 초기화 되는 코드
그런데, 오늘 신기한 녀석을 StaticOverflow - Is there a best practice for writing maps literal style in Java?에서 발견했다.
Instance Initialization Block
Map map = new HashMap() { { put("foo", "bar"); put(123, 456); } };
객체 초기화 블럭, 이런 녀석도 있었다.
정말 동작하는지 공식 문서를 찾아보니 The Java Tutorials - Initializing Fields에 떡하니 쓰여있는 내용
Normally, you would put code to initialize an instance variable in a constructor. There are two alternatives to using a constructor to initialize instance variables: initializer blocks and final methods. The Java Tutorials
즉, 생성자에서 변수를 초기화 하는 방법을 대신해서 객체의 변수 값을 초기화 하는 방법은 초기화 블럭 과 파이널 메소드 이다. 라고...
파이널 메소드 방법은 공식 문서 참조.
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- Java
- JavaScript
- 커피스크립트
- Git
- Eclipse
- REACTOR
- Commonjs
- Coffeescript
- Widget
- VI
- Node.js
- VIM
- 리액티브프로그래밍
- Windows
- maven
- 스프링인액션
- unit test
- tipoftheday
- Configuration
- 자바
- 리액터
- intellij
- 윈도우즈
- 빔
- React
- console
- reactiveprogramming
- dojo
- gvim
- 자바스크립트
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 |
글 보관함