When do instance variables get initialized and values assigned?

When the memory for the atribute 1 & 2 are allocated in the heap ?

The memory for the object as a whole is allocated when the new operator is invoked, before the java.lang.Object constructor is entered. Memory is allocated for individual Integer instances in init, but there is no point when memory is allocated for individual properties — only whole objects.

Curious to know why is attribute 2 is NULL ?

The init method is called in the super constructor, so attribute2 is assigned new Integer(200), and then the subclass constructor is invoked which applies property initializers in the order they appear in the source code. This line

private Integer attribute2 = null;

overwrites the value assigned by init() to null.

If you add a call to

 System.out.println("attribute 2 : " +attribute2);

right after your call to super(); then this will become apparent.

Are there any design flaws?

Calling sub-class methods before the base class has finished initializing is dangerous. The sub-class might rely on its base-class’s invariants to protect its own invariants, and if the base-class constructor has not completed, then its invariants may not hold.

This is also likely to confuse C++ programmers and the like who would expect a call to init from the base class to invoke the base class’s version since C++ rewrites the vtable pointer as constructors are entered.

See The Java Language Specification for all the gory details.

Leave a Comment