When does the Constructor get called?
Constructor is what practically creates object. It is called when object is created by executing new MyClass()
(or its parametrized version).
The object memory is allocated, the field variables with initial values are initialized, and then the constructor is called, but its code is executed after the constructor code of the object super class.
basically constructors are called to initialize the values of the instance variables except the case for default constructors. However, this initialization of the instance variables are done in 4 steps (as applicable):
Whenever we create an object by using 'new' operator then 1st task is performed by the new i.e. it allocates the memory for object in heap with pointing to the reference variable in stack and set the initial values of object fields.then it calls the constructor with passing 'this' as object to initialize it according to your requirement...
So the constructor is always called after the object creation....
Note: When you enter in constructor so 'this' keyword is working means your object has been created.