"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!"