could you help me please find out why the ouput of this code is "2 4 6 8 5" ?
After The two static (add(2) and add(4) ) it should run the "new Hello" initialization, so it should restart to run all the static order from the beginning. I think that it should start an infinite loop. WHere I wrong ?
Surprisingly no. Everything that belongs to the class (that is, static) gets executed first. This allows you to set up static members that may be needed in the constructor first. Then instance initializers for the same reason. Since includes may have initializers, they are executed from the top down.
All things are lawful, but not all things are profitable.
The reason you don't get an infinite loop of initalisations is that the static initialisers are only run once as part of class loading. They do not run every time an instance is created.
As already pointed out the constructor will be the last thing to run in your code. The static initialisers come first (in textual order), followed by the non-static initialisers (also in textual order) .