in what order are static blocks and static variables in a class executed? [duplicate]

From section 12.4.2 of the JLS, snipped appropriately:

The procedure for initializing C is then as follows:

  • Then, initialize the final class variables and fields of interfaces whose values are compile-time constant expressions (§8.3.2.1, §9.3.1, §13.4.9, §15.28).

  • Next, execute either the class variable initializers and static initializers of the class, or the field initializers of the interface, in textual order, as though they were a single block.

So for non-compile-time-constants, it's not a case of "all variables" and then "all static initializers" or vice versa - it's all of them together, in textual order. So if you had:

static int x = method("x");

static {
    System.out.println("init 1");
}

static int y = method("y");

static {
    System.out.println("init 2");
}

static int method(String name) {
    System.out.println(name);
    return 0;
}

Then the output would be:

x
init 1
y
init 2

Even making x or y final wouldn't affect this here, as they still wouldn't be compile-time constants.

P.S:Also noticed that string variable initialization happens before the block only when i insert the final modifier.

At that point, it's a compile-time constant, and any uses of it basically inlined. Additionally, the variable value is assigned before the rest of the initializers, as above.

Section 15.28 of the JLS defines compile-time constants - it includes all primitive values and String, but not the wrapper types such as Integer.


Here is a short and straight forward answer to you question....

static Variable :

static Variables are executed when the JVM loads the Class, and the Class gets loaded when either its been instantiated or its static method is being called.

static Block or static Initializer Block :

static static Initializer Block gets Initialized before the Class gets instantiated or before its static method is called, and Even before its static variable is used.

///////// Edited Part /////////

class NewClass {

    final static String string = "static";
    final static Integer integer = 1;

    static {
        System.out.println(NewClas.string + " " + NewClas.integer);
    }

    public static void main(String [] args) { // throws Exception
        new NewClas();
    }

}

The above will print static 1.

The reason is that the JVM will do the optimization process known as Constant folding, doing an pre-calculation of the constant variables.

Moreover in your case the result was static null cause Constant folding is applied to Primitive type and not Wrapper Object, in your case its Integer...


They are initialized in the given order (fields and static blocks), that's why printed value is null, nothing was assigned to static fields that are defined after the static block.