Reputation: 115
I learned that the principle for order of initialization is:
But I'm still confused by the output of this code:
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);
}
}
The result is:
2
4
6
8
11
5
Constructor!
12
Main method!
10
If without the statements of add(10); add(11); add(12); I can totally understand. Can you please explain the order of initialization for those 3 statements?
Upvotes: 10
Views: 2259
Reputation: 322
1) Block that does not have name like below is called "Instance Initializer" which only get called when new objects will be created its like DefaultConstructor or noArg Constructor.
{
add(11);
}
2) In above code you have Static Block (Which get called first at the Class Loading itself), Instance Initializer (Which get called while creating the object), Explicit DefaultConstructor (Which get called while creating the object but remember always Instance initializer takes priority) and last Main method.
3) Now lets analyze your code,
1st call:
static
{
add(2); //print 2
}
2nd call:
static {
add(4); // print 4
}
3rd 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 Instance Initializer, Explicit Default Constructor will be called.
public Test1() {
add(5); // print 5
System.out.println("Constructor!"); // print Constructor!
}
8th call: Again the last Static block will be called.
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
}
Upvotes: 8
Reputation: 22422
Whatever @DontPanic has answered is correct, the main point that I wanted to highlight is that the order (from top to bottom) of the code blocks in the source code also matters, you can look here from Java doc.
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.
Upvotes: 1
Reputation: 2375
In Java, for one class all static initializers are executed in order (top to bottom of a class) before all constructors. The terms within {} are add to the constructor, these are not static initializers (see http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html).
So instance 1 of Test1 :
static {
add(2);
}
...
static {
add(4);
}
Then, there is a constructor of Test1 inside a static block. It is called, so the instance member are initialized because previous static initializers have been called :
{
add(6);
}
...
{
add(8);
}
...
{
add(11);
}
After, the next operation in the constructor is called :
add(5);
Then we return in the first instance to have the last static initiazer to be called :
static {
add(12);
}
Finally, the class if fully initialized, so main method is called :
public static void main(String[] args) {
System.out.println("Main method!");
add(10);
}
So, output :
2
4
6
8
11
5
Constructor!
12
Main method!
10
Upvotes: 1
Reputation: 1367
Static initializer are the first, so you get
2
4
But in the next static initializer you call a new instance of Test1.class, so instance initializers are triggered, constructor is triggered after them and you get:
6
8
11
5
Constructor!
After that the rest of static initializers are called. so:
12
And the last is main method:
Main method!
10
Upvotes: 7
Reputation: 1189
I think you confused because (12) before (main method - 10)
That happens because main method called at the last but other initialization order are clear
Upvotes: 1