I have a testing code like this:
public class Constants {
public static String c1 = "C1";
static {
System.out.println("Constants Class Loaded!");
}
}
public class Test {
public static void main(String[] args) {
String c1 = Constants.c1;
System.out.println(c1);
}
}
Its output is:
Constants Class Loaded!
C1
So, the class Constants is loaded by JVM. But if I add a final keyword to the static field in class Constants:
public class Constants {
public static final String c1 = "C1";
static {
System.out.println("Constants Class Loaded!");
}
}
Its output changed to:
C1
It seems the class Constants is not loaded.
My local environment is:
OS: Win7 x64
JVM: JRockit (build R28.2.0-79-146777-1.6.0_29-20111005-1808-windows-ia32, compiled mode)
So, my questions are:
- Why reference to a static final field will not trigger class loading? What will JVM do(bytecode) when it meets this code?
- Is this behavior depending on specific JVM? Or this is a rule in Java language specification?
- What's the advantage & disadvantage of this?
Thanks.