Going through Java documentation I had a question about initializer blocks. The documentation says under section Initializing Instance Members:
The Java compiler copies initializer blocks into every constructor. Therefore, this approach can be used to share a block of code between multiple constructors.
To test this I tried to implement it as below.
class First{
{
System.out.println("A Block");
}
First(){
System.out.println("Inside default constructor");
}
First(String a){
this();
System.out.println("Inside parameterized constructor");
System.out.println("String = "+a);
}
{
System.out.println("B Block");
}
}
When I create an object of First class as per documentation, I was expecting the code in initializer blocks to be copied in both the constructors and printed twice, but the output I receive is as below:
A Block
B Block
Inside default constructor
Inside parameterized constructor
String = Test
Even though I am executing both the constructors. Could someone please explain what I seem to have misunderstood or is there something wrong with my execution which is causing it to not follow the documentation.