public class StaticTest {
private static StaticTest stObj=new StaticTest();
private static int VAR1=10;
private static final int VAR2=20;
public StaticTest() {
System.out.println("Var1 : "+VAR1);
System.out.println("Var2 : "+VAR2);
}
public static void main(String[] args) {
System.out.println("VAR1 after constrution : "+StaticTest.VAR1);
}
}
출력 :자바 차이
VAR1 : 0 변수 2를 대입 할 때 변수 : constrution 후 20 VAR1 10
이유 VAR1 및 VAR2이 다른 행동?
VAR2는 상수 값이지만 VAR1은 단순한 정적 변수입니다. 즉, stObj가 초기화 된 후 VAR1이 초기화됩니다. 그래서 VAR1은 첫 번째 호출시 0 [기본값 int 초기화 값]이고 두 번째 호출 후에는 10입니다. – dexametason