When does the Constructor get called?
- Before object creation.
- During object creation.
- After object creation.
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.
At the byte code level.
this
Note: The constructor at the byte code level includes the initial values for variables and the code in the Java constructor. e.g.
int a = -1;
int b;
Constructor() {
super();
b = 2;
}
is the same as
int a;
int b;
Constructor() {
super();
a = -1;
b = 2;
}
Also note: the super() is always called before any part of the class is initialised.
On some JVMs you can create an object without initialising it with Unsafe.allocateInstance(). If you create the object this way, you can't call a constructor (without using JNI) but you can use reflections to initialise each field.
It gets called at object creation. The memory must be reserved first for the object, otherwise the constructor code could not run. So maybe we could say after object creation. Also note that initialization code written in the class gets called before the constructor code.
public Ex {
int xVal = -1;
int yVal;
public Ex() {
// xVal is already -1.
//yVal defaults to 0.
}
}
THE JVM will first allocate the memory for your object, then initialize all fields, then invoke your constructor.
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):
After the Object creation
once an object is created using new operator like Student s = new Student(); first Student object is created, and then constructor is called to initialize the variable of the object
We can prove constructor is called after creating the object by using below code
here we are using instance block. And also instance block is executed before the constructor
so I am printing hash code in three places
all these three times hash code is equal, that means object is created before the constructor is executed
because having a hash code means, there must be an object. And if hash code printed inside both instance block and constructor is equal. that means object must be created before the constructor execution
The constructor gets called when a new object is created.
NewObject n = new NewObject();
public class NewObject {
public NewObject() {
// do stuff when object created
}
}
Hope this helps.
Constructor is what practically creates object. It is called when object is created by executing new MyClass()
(or its parametrized version).
Given those options, 1. Before object creation.
After the constructor finishes, the object has been created.
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.