Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I learned that the principle for order of initialization is:

  1. Superclass first (not discussed here in this case)
  2. Static variable declarations and static initialization blocks in the order of appearance
  3. Instance variable declarations and static initialization blocks in the order of appearance
  4. The constructor

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?

like image 954
NathanC Avatar asked Apr 01 '17 08:04

NathanC


People also ask

What is the order of initialization for data?

Generally, the order of execution is from top to bottom and left to right. But a rare condition arises where this rule fails is when the initializer list is used in class. In the initializer list, the order of execution takes place according to the order of declaration of member variables.

How do you initialize a variable in the main method in Java?

Declare them at the class level without the static qualifier. So all of those variables would be instance variables. Next step is to write all your variable initialization code in a constructor Rebellion(){} and then in the main method just create an object of type class.

In which sequence will be executed the initialization of class attributes in Java?

Initialization blocks run in the same order in which they appear in the program. Instance Initialization blocks are executed whenever the class is initialized and before constructors are invoked. They are typically placed above the constructors within braces.

In what order are class members attributes initialized?

Member variables are always initialized in the order they are declared in the class definition.

How do you initialize a class in main?

There are two ways to initialize a class object: Using a parenthesized expression list. The compiler calls the constructor of the class using this list as the constructor's argument list. Using a single initialization value and the = operator.


2 Answers

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 
like image 115
DontPanic Avatar answered Oct 12 '22 23:10

DontPanic


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
}
like image 43
Hemant Singh Bisht Avatar answered Oct 13 '22 01:10

Hemant Singh Bisht