Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: initialization sequence of object

There is a code which is given as a task for a junior Java developers. I use Java during five years and this piece of code completely confusing me:

public class Main {

    String variable;

    public static void main(String[] args) {
        System.out.println("Hello World!");
        B b = new B();
    }

    public Main(){
        printVariable();
    }

    protected void printVariable(){
        variable = "variable is initialized in Main Class";
    }
}

public class B extends Main {

    String variable = null;

    public B(){
        System.out.println("variable value = " + variable);
    }

    protected void printVariable(){
        variable = "variable is initialized in B Class";
    }
}

The output will be:

Hello World!
variable value = null

But if we change String variable = null; to String variable; we will have:

Hello World!
variable value = variable is initialized in B Class

The second output is more clear for me. So, as far as I know the sequence of inizialisation in Java like this:

  • We go to the root of the class hierarchy (for Java it is always Object class), when we come to this root parent class:
    • All static data fields are initialized;
    • All static field initializers and static initialization blocks are executed;
    • All non-static data fields are initialized;
    • All non-static field initializers and non-static initialization blocks are executed;
    • The default constructor is executed;
  • Then we repeat the procedure for the underlying child class.

Also there is post which describes the behavior of the this keyword in context of a superclass - Calling base class overridden function from base class method

Based on the rules given above, I assume to have sequence like this:

  1. We are going to create a new instance of class B;
  2. We go to the part class Main;
  3. Initialize main.variable with null;
  4. Then we move to the default constructor of class Main;
  5. Constructor calls method b.printVariable() in class Main; (Why doesn't it call main.printvariable? We don't have this key word here.)
  6. The field b.variable "variable is initialized in B Class"
  7. Now we come back to the class B;
  8. We should initialize field b.variable with null value, am I right?;
  9. The default constructor of class B executed

Please, can someone give a complete and full explanation of how this inheritance inizialisation sequence works. And why changing String variable = null; to String variable; leads to another output.

like image 468
INlHELL Avatar asked Feb 25 '13 17:02

INlHELL


People also ask

How is an object initialized in Java?

Instantiation: The new keyword is a Java operator that creates the object. Initialization: The new operator is followed by a call to a constructor, which initializes the new object.

What is sequence of object in Java?

A Sequence is a data structure containing musical information (often an entire song or composition) that can be played back by a Sequencer object. Specifically, the Sequence contains timing information and one or more tracks.

How do you initialize an object with an object?

To create an object of a named class by using an object initializer. Begin the declaration as if you planned to use a constructor. Type the keyword With , followed by an initialization list in braces. In the initialization list, include each property that you want to initialize and assign an initial value to it.


1 Answers

The sequence is:

  1. Main -> "Hello"
  2. Main -> new B()
  3. B() -> Main() -> b.printVariable() -> sets the variable
  4. Back to initialising B, so variable=null occurs.

So basically, the super object Main() is constructed before any intialisation events of class B. Which means variable=null occurs later. This makes sense as otherwise B could break the initialisation of Main.

Joshua Bloch covers a lot of good ground in his effective java book about how dangerous inheritance is to get right, I would recommend it.

like image 80
John at TimeStored Avatar answered Oct 16 '22 16:10

John at TimeStored