The compiler optimizes the built-in static final fields by inserting the value into the bytecode instead of calculating the value at run time.
When you start the JVM and load the class for the first time (this is done by the class loader the first time the class is accessed), any static blocks or fields are loaded into the JVM and become available.
Demonstration:
public class StaticDemo {
Output:
Class StaticDemo loading... ONE_DAY_IN_MILLIS=86400000 instanceCounter=0 Class StaticDemo loaded Starting StaticDemo instanceCounter=1 instanceCounter=2 instanceCounter=3
Note that "Starting StaticDemo" does not appear as the first line of output. This is because the class must be loaded before , the main method can be executed, which means that all static fields and blocks are processed in order.
source share