Question: Considering the standard java initialization order rules it is not clear to me why the following code

```
public class Foo {

    static { new Foo(); }

    static{ System.out.println("static code"); }

    { System.out.println("non static code"); }

    public Foo() { System.out.println("constructor"); }

    public static void main(String[] args) {}
}

```

outputs this:

```
non static code
constructor
static code

```

Answer: 
So, the static block will execute as soon as the class loader loaded the class. So, your first static block `static { new Foo(); }` execute first which further calls the constructor `new Foo();`. As per the java [docs](https://docs.oracle.com/javase/tutorial/java/javaOO/initial.html) the **non-static block will be copied to every constructor by the compiler** which means `System.out.println("non static code");` will be copied to the constructor `public Foo() { System.out.println("constructor"); }`. So, it will print `non static code` first then `constructor`. After the execution of first static block it will execute the second static block which prints the last `static code`.

After compilation your code looks similar to below code:

```
public class Foo {

    static { new Foo(); }

    static{ System.out.println("static code"); }

    public Foo() { 
       System.out.println("non static code");
       System.out.println("constructor"); 
    }

    public static void main(String[] args) {}
}

```
The JLS says that

* The static blocks and static variable initializations will be executed in program source code order when a class is initialized.
* The instance blocks and instance variable initializations will be executed in program source code order when an instance is initialized. This happens *after* super instance initialization and *before* the constructor body is executed.

In your example, the first static creates an instance of `Foo`. This cause the instance block for `Foo` to be executed *while* the first static block is being executed. So, the sequence is:

1. The class is loaded
2. Static initialization for `Foo` is triggered
3. The first static block is executed which does a `new Foo()`.
	1. The instance block is executed - prints "non static code"
	2. The constructor is executed - prints "constructor"
4. The first static block finishes.
5. The second static block is executed - prints "static code".
6. The main method is called.