当我在
main
中打印constant
时,static
块不会执行,但是当我打印stat
时它会执行。在Java中,static final
有什么重要性吗?package com.test.doubt;
class Doubt {
public static final int constant = 123;
public static int stat = 123;
static {
System.out.println("Static Block");
}
}
public class MyProgram {
public static void main(String[] args) {
System.out.println(Doubt.constant);
}
}
javac
编译器优化掉了Doubt
类的需要。 - Peter Lawrey