10

When does the Constructor get called?

  1. Before object creation.
  2. During object creation.
  3. After object creation.
Bohemian
  • 412,405
  • 93
  • 575
  • 722
Sushant
  • 635
  • 2
  • 8
  • 19

10 Answers10

24

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.

Luciano
  • 8,552
  • 5
  • 32
  • 56
  • I am not agree with `the field variables with initial values are initialized, and then the constructor is called` because the role of the constructor is to initialize the variables (as you said `field variables`) in advance, then how you can say the `field variables` are set and **then** the constructor is called which clearly means that the `constructor` is called after the `field variables` are set. Where did you read this can you please share the resource? I am surprised that still your answer is accepted and got 21 points. – Stack Overflow Dec 21 '18 at 07:03
  • @Luciano is effectively correct. According to the Java Language Specification (I'm looking at Java SE 8 https://docs.oracle.com/javase/specs/jls/se8/html/jls-12.html#jls-12.5), "the indicated constructor is processed to initialize the new object using the following procedure... 4. Execute the... instance variable initializers for this class... 5. Execute the rest of the body of this constructor." – Alan Feldstein Oct 07 '21 at 22:01
6

At the byte code level.

  1. An object is created but not initialised.
  2. The constructor is called, passing the object as this
  3. The object is fully constructed/created when the constructor returns.

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.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
4

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.
    }
}
Nicholas K
  • 15,148
  • 7
  • 31
  • 57
Thorn
  • 4,015
  • 4
  • 23
  • 42
2

THE JVM will first allocate the memory for your object, then initialize all fields, then invoke your constructor.

Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
  • I would have said creating the object initialises the fields. AFAIK, there is no way to see an object before after its created but before it has been intiialised with default values. – Peter Lawrey Mar 05 '12 at 13:28
  • If you use a debugger you can actually see the fields getting initialized one by one. But it is true that from an invoker point of view there is no way to see that happen and do something about it. – Guillaume Polet Mar 05 '12 at 13:33
  • I think its a mix of terminology. If you breakpoint the first point a constructor is called, all the fields will be `false`, `0`, or `null` Its not possible to see an uninitialised object like you can in C or C++ where all the fields have random values (based on what the memory had before) – Peter Lawrey Mar 05 '12 at 13:38
1

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):

  1. variables are initialized with default values (ints with 0, chars with u\0000 etc.)
  2. variables are initialized with explicit initialization values
  3. initialized with static blocks
  4. constructors are called
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
1

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

code to prove constructor is called after object creation

enter image description here

here we are using instance block. And also instance block is executed before the constructor

so I am printing hash code in three places

  1. Inside Instance block
  2. Inside Constructor
  3. Inside main mehtod

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

1

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.

Ocracoke
  • 1,718
  • 3
  • 24
  • 38
  • 1
    This does not really answer his question. – Thorn Mar 09 '12 at 18:07
  • Is it before, during, or after? Other answers tried to come at this from different angles. The question is subtle, requiring more than a simple definition of what a constructor is. – Thorn Mar 11 '12 at 00:12
  • I did have at the top of the answer that the constructor is called when (i.e. during) the new object is created. As far as I was concerned, the question only asked when, not requiring intimate detail as to how it works. Apologies, however, if I have mis-interpreted the requirements for the answer. – Ocracoke Mar 11 '12 at 14:11
0

Constructor is what practically creates object. It is called when object is created by executing new MyClass() (or its parametrized version).

AlexR
  • 114,158
  • 16
  • 130
  • 208
0

Given those options, 1. Before object creation.

After the constructor finishes, the object has been created.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
-1

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.

Sparkup
  • 3,686
  • 2
  • 36
  • 50
Avinash
  • 1
  • 1
  • I think you need to clarify 'object creation' into multiple parts: Memory allocation, field population, etc. The question itself is vague, but this answer doesn't really clarify anything yet. – JBCP Mar 27 '14 at 20:30
  • when run method call in life cycle of thread? I mean in which state either in ready to run state or in running state? – Avinash Mar 30 '14 at 20:31
  • The above question was about objects in general, not Threads, so I'm not exactly sure what you are asking. – JBCP Mar 31 '14 at 15:57