public class BaseDay {
// 기준일자
public static final String STATIC_FINAL_DAY = "220714";
public static String STATIC_DAY = "220714";
}
public class BaseDayTest {
public static void main(String[] args) {
System.out.println(BaseDay.STATIC_DAY);
System.out.println(BaseDay.STATIC_FINAL_DAY);
}
}
이때 당연하게 결과값으로는 다음과 같다.
220714
220714
하지만, 이때 BaseDayTest를 디컴파일러로 돌려보면 다음과 같다.
public class BaseDayTest {
public static void main(String[] args) {
System.out.println(BaseDay.STATIC_DAY);
System.out.println("220714");
}
}
즉, static final은 컴파일러가 자동으로 치환하여 컴파일 한다는 것을 알 수 있다.
static final 은 컴파일러가 자동으로 치환하여 class파일을 만든다. 따라서, 상수만 따로 배포하는 프로그램을 짜고 싶을때는 static final 보다 단순한 static을 사용하자.
(물론 약간의 성능차이는 발생한다.)