서비스 클래스에서 constant 로 사용하기 위해 아래와 같이 변수를 선언했다
private final List<String> ALPA_LIST = Arrays.asList("a", "b", "c")접근제어자가 private 이고, 하나의 controller 에만 주입되어 사용하고 있어
static 키워드 없이 사용하였는데,
해당 변수에 대한 사용의도(private final이 constant로 적합한가?)에 대해 얘기를 하게 되었고
아래와 같은 의문을 갖게 되었다.
means there is only one copy of the variable in memory shared by all instances of the class.
The final keyword just means the value can’t be changed. Without final, any object can change the value of the variable.
결론은…
constant 로서 사용되는 변수는
초기화 후에 immutable 한 값이므로 static 키워드를 추가하여 사용한다.
private final static List<String> ALPA_LIST = Arrays.asList("a", "b", "c")Arrays.asList()
# remove(), add()를 지원하지 않음. 사용시 java.lang.UnsupportedOperationException 발생
# ArrayList와 마찬가지로 set(), get(), contains() 제공즉 value가 변할 수 있는 객체이므로, 상수로서 적합하지 않다. 따라서 immutable 하도록 아래와 같이 사용한다.
private final static List<String> ALPA_LIST = Collections.unmodifiableList(Arrays.asList("a", "b", "c"))Java 9부터는 List
@Test(expected = UnsupportedOperationException.class)
public final void givenUsingTheJava9_whenUnmodifiableListIsCreated_thenNotModifiable() {
final List<String> list = new ArrayList<>(Arrays.asList("one", "two", "three"));
final List<String> unmodifiableList = List.of(list.toArray(new String[]{}));
unmodifiableList.add("four");