It will sometimes - it depends on whether the variable is actually a constant:
- It has to be either a string or a primitive variable (possibly any other class with a null value; I'd have to check)
- The initialization expression has to be a constant expression
If that's the case, any references to the variable are effectively turned into the value. So in this code:
class Foo {
public static final int BAR = 5;
}
class Baz {
public static void quux() {
System.out.println(Foo.BAR);
}
}
The method in Baz is compiled into the same code as:
public static void quux() {
System.out.println(5);
}
There's no hint of Foo.BAR left in the bytecode, therefore Foo doesn't need to be initialized when the method executes.
If you want to prevent that from happening, you always just make it not be initialized with a constant expression in a variable initializer. For example:
class Foo {
public static final int BAR;
static {
BAR = 5;
}
}
class Baz {
public static void quux() {
System.out.println(Foo.BAR);
}
}
That would be enough to make Foo.BAR not count as a constant as far as the compiler is concerned.