Static block in Java not executed

  1. A static final int field is a compile-time constant and its value is hardcoded into the destination class without a reference to its origin;
  2. therefore your main class does not trigger the loading of the class containing the field;
  3. therefore the static initializer in that class is not executed.

In specific detail, the compiled bytecode corresponds to this:

public static void main(String arg[]){    
    System.out.println("**MAIN METHOD");
    System.out.println(9090)
    System.out.println(9190)
}

As soon as you remove final, it is no longer a compile-time constant and the special behavior described above does not apply. The Mno class is loaded as you expect and its static initializer executes.

Leave a Comment