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: 
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.
Well, to refactor the code, do the following steps:

1) remove non-static initialisation block - the compiler puts it **before** the code in the actual constructor

```
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) {}
}

```

2) Join static initialisation blocks - see [here](https://docs.oracle.com/javase/tutorial/java/javaOO/initial.html):

> 
> A class can have any number of static initialization blocks, and they
>  can appear anywhere in the class body. The runtime system guarantees
>  that static initialization blocks are called in the order that they
>  appear in the source code.
> 
> 
> 

```
public class Foo {

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

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

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

```

3) From there, it should be obvious.