티스토리 뷰

JAVA

궁금 궁금 (class variable 초기화)

Better than alone 2018. 3. 29. 10:18

class A{

List<Integer> a; //1 setter에서 new~~ 해주기

List<Integer> a = new ArrayList();  //2

}


1,2 중 뭐가 나은거지




==> 답변.

There is no difference - the instance variable initialization is actually put in the constructor(s) by the compiler.

The first variant is more readable.

You can't have exception handling with the first variant.

There is additionally the initialization block, which is as well put in the constructor(s) by the compiler:


{

    a = new A();

}

반응형

'JAVA' 카테고리의 다른 글

static 변수와 static 메소드  (0) 2018.04.05
openCSV  (0) 2018.04.02
lombok 롬복 이클립스에 설치  (0) 2018.03.29
JVML - Java Virtual Machine Language  (0) 2018.03.20
Inheritance  (0) 2018.03.16
댓글