EDIT: It turns out JLS is accurate, although it's hard to read. All this is described in detail in section 12.5 :
Before the result is a reference to the newly created object, the specified constructor is processed to initialize the new object using the following procedure:
Assign constructor arguments to the newly created parameter variables for this constructor call.
If this constructor starts by explicitly calling the constructor (ยง8.8.7.1) of another constructor in the same class (using this), then evaluate the arguments and process the constructor call recursively using these five steps. If the constructor call terminates abruptly, then this procedure terminates abruptly for the same reason; otherwise, go to step 5 .
This constructor does not start by explicitly calling the constructor of another constructor in the same class (using this). If this constructor belongs to a class other than Object, then this constructor will start by explicitly or implicitly calling the constructor of the superclass (using super). Evaluate the arguments and handle the call to the superclass constructor recursively using these five steps. If the constructor call terminates abruptly, then this procedure terminates abruptly for the same reason. Otherwise, go to step 4.
Run instance initializers and instance variable initializers for this class, assigning the initializer values โโof the instance variable to the corresponding instance variables in the order from left to right, in which they are displayed in text form in the source code for the class. If the execution of any of these initializers results in an exception, then no new initializers are processed, and this procedure terminates abruptly with the same exception. Otherwise, go to step 5.
Run the rest of the body of this constructor. If this execution completes abruptly, then this procedure terminates abruptly for the same reason. Otherwise, this procedure is performed normally.
Pay attention to the highlighted part - the chain constructor is executed, and then we will skip step 4, which will execute the instance initializer.
The reality is that instance initializers and fields are executed only once, as you can find out from your output.
Informally, I believe that it is accurate to describe the procedure as follows:
- Keep the chain constructors inside the same class (
this(...) ) until you reach the body of the constructor, which does not start with this . - Run the corresponding super constructor
- Run instance variable initializers and instance initializers
- Run the body of the "innermost" constructor
- Keep popping the stack of constructor bodies until you finish the entry constructor
Is this using MEANS with this () inside the constructor of the subclass, will it be implicit to remove the default call to the constructor without arguments to the superclass?
Yes. Somewhere in your constructor chain inside the class, you are guaranteed to get a constructor that calls super implicitly or explicitly. This is the only superclass constructor called.
EDIT: Please note that the tutorial you provided is clearly incorrect.
Class Example:
public class Test { { System.out.println("Foo"); } public Test() { } public Test(int i) { this(); } }
Exiting javap -c :
public class Test { public Test(); Code: 0: aload_0 1: invokespecial #1
As you can see, the constructor for Test(int) does not have code for the instance constructor compiled in it.
In principle, only constructors that directly call superclass constructors have instance initializer code in them. All other constructors will cause the instance initializer code to be executed through the constructor, which, of course, calls the superclass constructor.