Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is an object created first and then it's constructor executed?

My question is that is

A a=new A();

here is the object created first and then the constructor method is called? Or the object is created because the constructor method is called?

If the object is required to call the constructor, that means that the object should be created before the constructor is called and if I declare the constructor to be private (just to see if it works without the constructor), then I get an error.

Now if the constructor execution creates the object, I fail to understand how that logically works, I mean that without an object how can a method of the object can execute?

Can someone explain this?

like image 938
Chintan Shah Avatar asked Jun 23 '14 13:06

Chintan Shah


People also ask

What is the use of a constructor in a class?

It is used to declare/initialize the common part of various constructors of a class. Constructors : are used to initialize the object’s state. Like methods, a constructor also contains collection of statements (i.e. instructions) that are executed at time of Object creation.

What is the Order of execution of constructors in Java?

Therefore the execution of the constructors starts after the object initialization. It follows a certain sequence according to the class hierarchy. There can be different orders of execution depending on the type of inheritance. 1. Order of execution of constructor in Single inheritance

Why is the parameterized constructor called first in Java?

In the above code, the parameterized constructor is called first even when the default constructor is called while object creation. It happens because this keyword is used as the first line of the default constructor. 4. Calling superclass constructor using super keyword

What is the Order of execution of initialization blocks and constructors?

Order of execution of Initialization blocks and Constructors in Java. In a Java program, operations can be performed on methods, constructors and initialization blocks. Instance Initialization Blocks : IIB are used to initialize instance variables. IIBs are executed before constructors. They run each time when object of the class is created.


3 Answers

Memory is allocated for the object before the constructor is called, yes.

You can think of it in terms of the following steps:

  1. Memory is allocated for the object.
  2. Various constructors are executed (always from the top of the hierarchy down to the most specific one, e.g., Object's constructor is executed first, then whatever other super-classes you have, then the actual class's).
  3. So the object already has memory for it, and it's being initialized by the constructors, not created. The constructors are simply setting up the state of the object.
  4. The object is unusable until the constructors are finished executing.

Point 4 actually isn't entirely true, because you can leak a reference to the object via passing this to another method within the constructor, but that's a bit of a fringe case. It's kind of a fascinating phenomenon, because you can access things like final variables before they're initialized and retrieve two different values from them, depending on where in the execution they are.

Addressing the comments below: the constructor itself doesn't return anything. It has a void return type. How the variable actually receives the object isn't as straightforward as you might think, and it's a really good question. You can read very detailed answers about this subject on the following Stack Overflow answers, which do a much better and thorough job than I could replicate here.

  • Can we have a return type for a constructor in Java?
  • Why do constructors in Java not have a return type?

As a side note, as Peter Lawrey mentions in the comments, you are able to use the Unsafe API to create instances of an object without executing a constructor. Based on some other Stack Overflow discussions, however, the general opinion seems to be that anything you do with Unsafe doesn't really qualify as normal Java behavior.

like image 80
asteri Avatar answered Oct 26 '22 23:10

asteri


You tagged the question with more than one language, you should pick one.

But the convention is to first allocate memory for the object and its fields, then run the constructor and then, if applicable, assign a reference to this new object to the variable a.

Some nuances exist. C#, for example, does things a little differently for value types.

  1. Allocate memory for value type instance
  2. Run constructor
  3. Copy object, bit-by-bit, to the location a is pointing to.

This makes sure a never points to a partially-constructed instance of a value-type. It also makes sure that, if the constructor throws an exception, the memory location a points to is not left corrupted.

like image 40
dcastro Avatar answered Oct 27 '22 00:10

dcastro


Answering for C++:

A new-expression (let's say new A()) is compiled as follows:

1 Check that the default constructor of A is accessible within the context; otherwise, signal a compilation error.

2a If class A has an overloaded allocation function A::operator new(), call it to obtain space for the object.

2b Otherwise, call the allocation function ::operator new() to obtain space for the object.

3 Call the default constructor A::A() in the space obtained from the allocation function.

like image 30
Angew is no longer proud of SO Avatar answered Oct 26 '22 23:10

Angew is no longer proud of SO