CodeGym /Java Course /Module 2. Java Core /Creating objects: the order of constructor calls

Creating objects: the order of constructor calls

Module 2. Java Core
Level 8 , Lesson 0
Available

"Hello, Amigo! Now I'm going to tell you about how objects are created."

"What's so complicated about it, Uncle Rishi? You write new and the class name, indicate the correct constructor, and you're done!"

"That's true. But what happens inside the object when you do that?"

"What happens?"

"This is what happens: The object is created in several stages."

1) First, memory is allocated for all the class's member variables.

2) Then the base class is initialized.

3) Then all the variables are assigned values, if any are specified.

4) Finally, the constructor is called.

"It doesn't look very difficult: first the variables, then the constructor."

"Let's see how this works in an example with two classes:"

Code Description
class Pet
{
 int x = 5, y = 5; ←-
 int weight = 10; ←-

 Pet(int x, int y)
 {
  this.x = x; ←-
  this.y = y; ←-
 }
}
class Cat extends Pet
{
 int tailLength = 8; ←-
 int age;
 Cat(int x, int y, int age)
 {
  super(x, y); ←-
  this.age = age; ←-
 }
}
Declare two classes: Pet(pet) and Cat(cat).

In the Cat class, we see an explicit call to the base class's constructor.
It should always be on the first line in the constructor.

Here's what happens after the memory is allocated:
18 – call the constructor of the base class.
3, 4 – initialize variables in Pet.
8, 9 – execute the code in the Pet constructor.

Then the Cat class begins to be initialized.
14 – initialize variables in Cat
19 – execute the code in the Cat constructor

public static void main(String[] args)
{
 Cat cat = new Cat (50, 50, 5);
}

"That's a little confusing. Why is it so complicated?"

"It's actually not difficult if you know what's really going on:"

If a class does not have any constructors, one will be created automatically.

Default constructor
class Cat
{
 int x = 5;
 int y = 5;
}
class Cat
{
 int x = 5;
 int y = 5;
 public Cat() 
 {
 }
}

If you don't call the base class constructor, it will be called automatically.

Call of the base class's constructor
class Pet
{
 public String name;
}
class Pet extends Object
{
 public String name;
 public Pet()
 {
  super();
 }
}
class Cat extends Pet
{
 int x = 5;
 int y = 5;
}
class Cat extends Pet
{
 int x = 5;
 int y = 5;
 public Cat()
 {
  super();
 }
}

Member variables are initialized in the constructor.

Initialization of member variables
class Cat
{
 int x = 5;
 int y = 5;
}
class Cat
{
 int x;
 int y;
 public Cat()
 {
  super();
  this.x = 5;
  this.y = 5;
  }
}
What really happens
class Pet
{
 int x = 5, y = 5;
 int weight = 10;
 Pet(int x, int y)
 {
  this.x = x;
  this.y = y;
 }
}

class Cat extends Pet
{
 int tailLength = 8;
 int age;
 Cat(int x, int y, int age)
 {
  super(x, y);
  this.age = age;
 }
}
class Pet extends Object
{
 int x;
 int y;
 int weight;

 Pet(int x, int y)
 {
  //call of the base class's constructor
  super();
  //initialize variables
  this.x = 5;
  this.y = 5;
  this.weight = 10;
  //execute the constructor code
  this.x = x;
  this.y = y;
 }
}
class Cat extends Pet
{
 int tailLength;
 int age;
 Cat(int x, int y, int age)
 {
  //call of the base class's constructor
   super(x, y);
  //initialize variables
  this.tailLength = 8;
  //execute the constructor code
  this.age = age;
 }
}

"Now it's much more clear: first the base class, then variables outside the constructor, then the constructor code."

"Well done, Amigo! That's it!"

Comments (18)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Hoist Level 35, San Diego, United States
29 September 2023
10 May 2020
Can somebody explain this?

1) First, memory is allocated for all the class's member variables.  -even for a child class?

2) Then the base class is initialized.

3) Then all the variables are assigned values, if any are specified.

4) Finally, the constructor is called.

Declare two classes: Pet(pet) and Cat(cat).

In the Cat class, we see an explicit call to the base class's constructor.
It should always on the first line in the constructor.

Here's what happens after the memory is allocated:
18 – call the constructor of the base class. constructor is called, even though child class isnt initialized?
3, 4 – initialize variables in Pet.
8, 9 – execute the code in the Pet constructor.

Then the Cat class begins to be initialized. - How can it be initialized here, when before we used it's constructor, to call parent class?
14 – initialize variables in Cat
19 – execute the code in the Cat constructor
Vidhya Level 16, Sunnyvale, United States
31 May 2020
When we say new Class() in java what JVM does is 1. Check the inheritance tree of that class (All the parent classes for this class, this includes Object class) 2. Then it starts the allocation of memory for the variables. 3. Then it starts with creation of the Super class followed by the child class(which means all the constructors of the super class starts to run and when it reaches the final child class and then a new object creation is complete . We can think like a stack of plates. When we run the Cat class , Cat Constructor runs then it calls the super constructor implicitly/explicitly now the first plate in the stack is Cat and then next Plate is Pet class. When the Pet constructor runs it calls implicitly the Object class which the parent of all the classes.Now the next plate is Object .Once Object constructors runs then it will be taken out and so are others). So any class to be created all the parents have to be created.
Andriy Level 22, Toronto, Canada
5 September 2019
"You can't complete this task: you don't have access to it." ...
Isenraf Level 20, Douala, Cameroon
9 July 2019
don't have access to the code entry task, can someone tell me why?
Muhammad Vahhaaj Level 19, Rawalpindi, Pakistan
12 July 2019
yea, strange
Koniasz Level 22, Warszawa, Polska
22 August 2019
Lol, I had the same but when I go "Next lesson" and then go previous the code entry exercise was available.
Hashirama Level 26, Port-Harcourt, Nigeria
7 February 2019
Guys I have a question: Are the objects created immediately when we use the new keyword; at compile time or After, at runtime?
Ivaylo Trifonov Level 25, Madrid, Spain
15 May 2019
Good question! I am curious about the answer.
Oleg Tokarenko Level 19, L'viv, Ukraine
21 May 2019
objects are allocated on the heap dynamically i.e. at runtime, while primitives are allocated on the stack i.e. at compile-time.
Hashirama Level 26, Port-Harcourt, Nigeria
28 June 2019
Thanks. This is correct!
Fadi Alsaidi Level 34, Carrollton, TX, USA
25 January 2020
anyone can explain to me why is that important?
Jonaskinny Level 25, Redondo Beach, United States
2 March 2022
could maybe figure out footprint needed for primitives, ==t a warning that crazy deep hierarchies, to much object composition / object attributes etc. might bite you at runtime on full data / load after the system has been designed and developed.
Benjamin Winchester Level 17, Savannah, United States
4 December 2018
Example code uses Cat and Pet classes, example narrative uses Cat and Animal classes. Should update to be consistent.
Sindhura Level 15, Delhi, India
20 October 2018
Why the code entry task is locked for me?Is it same for everyone?
Ahmed Saeed Level 18, Baunatal, Germany
4 November 2018
Go to the next lesson, then come back.
Benjamin Winchester Level 17, Savannah, United States
4 December 2018
Same happened to me, its a bug. Ahmed's suggestion worked.
Ramilya Level 20, SPb, Russian Federation
16 April 2019
It's locked for me too.