Good day colleagues!

In the literature they write that when a class is loaded, the static block is first executed, and then the dynamic block and the constructor. I managed to write a class so that the static block was executed last.

public class Singleton { static Singleton st = new Singleton(); static { //статичСский Π±Π»ΠΎΠΊ System.out.println("БтатичСский Π±Π»ΠΎΠΊ"); } { //Π°Π½ΠΎΠ½ΠΈΠΌΠ½Ρ‹ΠΉ Π±Π»ΠΎΠΊ System.out.println("ДинамичСский Π±Π»ΠΎΠΊ"); } Singleton(){ //конструктор System.out.println("ΠšΠΎΠ½ΡΡ‚Ρ€ΡƒΠΊΡ‚ΠΎΡ€"); } public static void main(String[] args) { //ΠŸΡƒΡΡ‚Π°Ρ Ρ‚ΠΎΡ‡ΠΊΠ° Π²Ρ…ΠΎΠ΄Π° } } 

OUTPUT:

 ДинамичСский Π±Π»ΠΎΠΊ ΠšΠΎΠ½ΡΡ‚Ρ€ΡƒΠΊΡ‚ΠΎΡ€ БтатичСский Π±Π»ΠΎΠΊ 

Question: How to explain the observed behavior?

    2 answers 2

    Let's take a look:

    Your first static field is declared:

     static Singleton st = new Singleton(); 

    It needs to be initialized, respectively, the constructor is called:

     Singleton() { System.out.println("ΠšΠΎΠ½ΡΡ‚Ρ€ΡƒΠΊΡ‚ΠΎΡ€"); } 

    But before the constructor is called, there is a dynamic initialization block, so the object is first initialized in this block, and then the constructor is executed:

     { //Π°Π½ΠΎΠ½ΠΈΠΌΠ½Ρ‹ΠΉ Π±Π»ΠΎΠΊ System.out.println("ДинамичСский Π±Π»ΠΎΠΊ"); } //Π·Π°Ρ‚Π΅ΠΌ вызываСтся нСпосрСдствСнно конструктор 

    So, the first line of the class is ready, the static field static Singleton st initialized, then we already initialize the static block. Can you, on the basis of my answer, suppose what happens if you enter another call to the Singleton constructor in a static block? Assume, and then you can try and compare your guess.

    UPD.

    This is not to say that the static block is called before the dynamic block. They are called under different conditions. The static block is called after the class loader is loaded by the class, and the dynamic block is called when the class is instantiated . That is static - class initialization; dynamic - class instance initialization. And the dynamic block in your particular example is called during the initialization of the Singleton instance in the first line, which is a static field.

    • if static -> dynamic -> constructor were called, everything would be clear to me. Maybe explain why static is not called before the dynamic block? After all, if you remove the static field, and create an object in the Maine, the order will be exactly this: static -> dynamic -> constructor. - Andrew Kachalin
    • You yourself answered your question. Initially, static variables are initialized, then static blocks, etc. ... - Oleksiy Morenets
    • @OleksiyMorenets So why the static block was not initialized in the first place? What, when initializing a statistical field, do you need not to perform a static block at all? - Andrew Kachalin
    • one
      Change the static block and static variable in some places and you will be very surprised :) - learp
    • one
      Yes, I just added this answer now. Rather, to say that the program began with the initialization of a static field, which contains an object that needs to be initialized. - iksuy

    Initialization of static fields and static blocks are performed in the order they are declared in the class. Since you have static static field static Singleton st = new Singleton(); is declared first, it is initialized first. When a field is initialized, a class object is created, but the execution of static block when creating an object is skipped, because its execution will start only after initializing the static field declared first. Accordingly, when initializing this field, the static block is ignored, the Dynamic block is executed, then the class constructor, the field is now initialized, and, in accordance with the declaration order, the static block is executed. http://www.quizful.net/post/java-fields-initialization

    • Yes, yes, we already understood. - Andrew Kachalin