Good day.

Studying java tests, I noticed some inconsistency. In the first question, "specify the output":

public class QTest { { System.out.print("1"); } public static void main(String[] args) { System.out.print("2"); new QTest(); } static { System.out.print("3"); } } 

the correct answer is "321", that is, after the static block follows the constructor call and, finally, the call to the non-staic block.

In the second question of a similar plan:

 public class Test { { System.out.println("1"); } Test () { System.out.println("2"); } static { System.out.println("3"); } { System.out.println("4"); } public static void main(String[] args) { new Test(); } } 

the correct answer is "3142", that is, the call to non-static blocks precedes the call to the constructor. What could be the difference?

Thank you.

    1 answer 1

    A static initialization block is called when the class is first accessed, regardless of whether you instantiated it or called the static method. Those. in fact, right after the class has been loaded into the classLoader 's RAM. Therefore, it has nothing to do with instances of a class, and even more so with ordinary initialization blocks.

    The initialization block is called when creating an object instance , after calling the parent constructors, and before calling the constructor of this class. Those. the super call in the constructor of this class will be executed before the initialization blocks are called, and the rest of the constructor - after . If there is no super call in the constructor of this class, it still happens (implicitly). In fact, the initialization block has the same priority as the field. And they are initialized in turn.

    The constructor is called when all fields of the object are defined (in java, any field of the object will be determined when creating an instance with its default value, even if this is not done explicitly).

    And yes. In the first example, your 2 is not displayed in the constructor , but in the main method . And when the constructor is called from the main method, the fields of the new object are initialized and initialization blocks are called, then the constructor itself is called, and after that the main method code with the already created instance continues to be executed.

    Something like this. Read about initializer block , static initializer block on the Internet.

    • Thank you very much. Then, if in the first example, 2 were not displayed in the framework of the main() method, but in the framework of the QTest() constructor, we would get the output: 312 , right? - Dmitry08
    • Yes, that's right, added in reply - selya
    • Yes, I fully understood this issue. Thank you. - Dmitry08
    • one
      A little bit correct. The initialization block is called not quite up to the constructor. Each default constructor contains an implicit call to super() , so the initialization block is called after calling this super() . - Pollux
    • Yes, I forgot about inheritance, summarized and corrected the answer - selya