1 public class StaticInitializer { 2 static int j = getI(); 3 static int i = 1; 4 5 static int getI () { 6 return i; 7 } 8 9 public static void main(String[] args) { 10 System.out.println(StaticInitializer.j); 11 System.out.println(StaticInitializer.i); 12 } 13 }
以上程序运行结果:
1 public class StaticInitializer { 2 static int j = getI(); 3 static int i = 1; 4 5 static int getI () { 6 return i; 7 } 8 StaticInitializer() 9 { i=4; 10 } 11 public static void main(String[] args) { 12 System.out.println(StaticInitializer.j); 13 System.out.println(StaticInitializer.i); 14 } 15 }
以上程序运行结果:
以上说明:静态变量(类变量)的值不能被构造函数改写
时间: 2024-11-11 03:04:36