Here another rule of thumb concerning constructors and "instance init block" - I want to emphasize this because taking several mock exams, I was unable to answer to such questions spontaneously: what comes first - the instance init block or the constructor? How is this related to the call to super() or this()?
There has been one crucial hint which helped me to get a deeper understanding: an instance init block can reduce redundancy ... provided that several constructors (the overloaded ones in the same class) have to share a member.
Consequently, it's obvious that a constructor has to do his work AFTER the instance init block has finished.
Below, please find an example in which a variable called
constructorCalls is accessed by multiple constructors. If the instance init block was called only after completion of a constructor, such approach of sharing variables would not make sense.
And here's the ouput:
static init block
FatherOfMultipleConstructors() has been invoked
instance init block
MultipleConstructors(int i, int j) can now access variables from instance init block
MultipleConstructors(int i) can now access variables from instance init block
MultipleConstructors() can now access variables from instance init block
constructorCalls has been accessed by ... MultipleConstructors(int i, int j) MultipleConstructors(int i) MultipleConstructors()
As you can see in the ouput, the instance init block is called after all constructor's calls to super() (or this()) and before the constructors have finished their work.
Hope this explanation is not too confusing...