Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is a constructor executed?

I am making some revisions from the lecture slides and it says a constructor is executed in the following way:

  1. If the constructor starts with this, recursively execute the indicated constructor, then go to step 4.

  2. Invoke the explicitly or implicitly indicated superclass constructor (unless this class is java.lang.Object).

  3. Initialise the fields of the object in the order in which they were declared in this class.

  4. Execute the rest of the body of this constructor.

What I don't understand is that a constructor can never "start" with this, because even if it forms no class hierarchy/relationship then super() is inserted by default.

How would this fit in with the description above?

like image 261
simion Avatar asked Jun 01 '10 10:06

simion


People also ask

Is constructor executed automatically?

It is executed automatically whenever an object of a class is created. The only restriction that applies to the constructor is that it must not have a return type or void. It is because the constructor is automatically called by the compiler and it is normally used to INITIALIZE VALUES.

How is constructor executed in Java inheritance?

When a drived class is extended from the base class, the constructor of the base class is executed first followed by the constructor of the derived class.

When constructor will be executed explain?

The constructor is called when an object of a class is created. It can be used to set initial values for object attributes.

What is a constructor and when is it executed Java?

A Java constructor is special method that is called when an object is instantiated. In other words, when you use the new keyword. The purpose of a Java constructor is to initializes the newly created object before it is used.


2 Answers

A constructor (for every class except java.lang.Object) has to start with either "super()", to call its superclass' constructor, or "this()", to call another constructor of the same class. If you don't include either of those in your constructor the compiler will insert a call to super(). It's fine for a constructor to start with a call to another constructor in the same class, as long as eventually a constructor in the class gets called that calls a superclass constructor.

like image 183
Nathan Hughes Avatar answered Oct 06 '22 01:10

Nathan Hughes


I don't think you're right, or I don't understand the problem. From the Java Language Spec:

If a constructor body does not begin with an explicit constructor invocation and the constructor being declared is not part of the primordial class Object, then the constructor body is implicitly assumed by the compiler to begin with a superclass constructor invocation "super();", an invocation of the constructor of its direct superclass that takes no arguments.

As such a constructor can start with this(...) which calls another constructor of the same class. Only when a constructor is called which does not start with either this(...) or super(...), super() is called automatically.

What I would say is that after an object is constructed super(...) has been called (if the class is not java.lang.Object).

like image 34
extraneon Avatar answered Oct 06 '22 02:10

extraneon