In Java, what is the order of initialization of these statements after the main method

I found out that the principle for the initialization order is:

  • Superclass at the beginning (not discussed here)
  • Static variable declarations and static initialization blocks in order of appearance
  • Instance variable declarations and static initialization blocks in order of appearance
  • Constructor

But the output from this code is still confusing to me:

public class Test1 { static { add(2); } static void add (int num) { System.out.println(num + " "); } public Test1() { add(5); System.out.println("Constructor!"); } static { add(4); } { add(6); } static { new Test1(); } { add(8); } public static void main(String[] args) { System.out.println("Main method!"); add(10); } { add(11); } static { add(12); } } 

Result:

 2 4 6 8 11 5 Constructor! 12 Main method! 10 

If without statements add (10); Add (11); Add (12); I can fully understand. Could you explain the initialization order of these three expressions?

+7
java initialization
source share
5 answers

1) A block that does not have a name like the one below is called an β€œ Initializer of an instance ”, which is called only when new objects are created, such as DefaultConstructor or noArg Constructor.

 { add(11); } 

2) In the above code, you have a Static block (which is called first in the class loading itself), Instance Initializer (which is called when the object is created) Explicit DefaultConstructor (which is called when the object is created, but always remembers the instance initializer takes precedence) and the last method Main

3) Now we will analyze your code,

1st call:

 static { add(2); //print 2 } 

Second call:

 static { add(4); // print 4 } 

Third call:

 static { new Test1(); // Here Object is getting created so all Instance Initialzer will be called first in a sequential manner. } 

4th call:

 { add(6); // print 6 } 

5th call:

 { add(8); // print 8 } 

6th call:

 { add(11); // print 11 } 

7th call: After the instance initializer, the Explicit default constructor is called.

 public Test1() { add(5); // print 5 System.out.println("Constructor!"); // print Constructor! } 

8th call: The last static block will be called again.

 static { add(12); // print 12 } 

9th call: Finally, the main method will be called

 public static void main(String[] args) { System.out.println("Main method!"); // print Main method! add(10); // print 10 } 
+6
source share

The static initializer is the first, so you get

 2 4 

But in the next static initializer, you call a new instance of Test1.class, so the instance initializers start, after them the constructor starts, and you get:

 6 8 11 5 Constructor! 

After that, the rest of the static initializers are called. So:

 12 

And the latter is the main method:

 Main method! 10 
+6
source share

I think you are confused because (12) used to be (the main method is 10)

This is because the main method called in the last but different initialization order is transparent

+1
source share

In Java, for a single class, all static initializers are executed in order (from top to bottom of the class) before the all constructors. The terms inside {} are added to the constructor; these are not static initializers (see http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html ).

So, instance 1 from Test1:

 static { add(2); } ... static { add(4); } 

Then in the static block there is the constructor Test1. It is called, so the instance element is initialized because the previous static initializers were called:

 { add(6); } ... { add(8); } ... { add(11); } 

After that, the following operation is called in the constructor:

 add(5); 

Then we return in the first case to invoke the last static initiazer:

 static { add(12); } 

Finally, the class, if it is fully initialized, so the main method is called:

 public static void main(String[] args) { System.out.println("Main method!"); add(10); } 

So the conclusion is:

 2 4 6 8 11 5 Constructor! 12 Main method! 10 
+1
source share

Regardless of what @DontPanic answered correctly, the main point I wanted to highlight is that the order (top to bottom) of code blocks in the source code also matters , you can see here from the Java document.

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

+1
source share

All Articles