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();

}

반응형