If one constructor calls another constructor which is the one that allocates the memory for the object

Viewed 69

Let's consider following code:

class Auto{
    String color;
    int mileage
    public Auto (String color){
       this.color=color;
    }
    public Auto (String color, int mileage){
       this(color);
       this.mileage=mileage;
    }
}

which Constructor is the one that "constructs" the object and what does the other one do?

why is this allowed ? what are the benefits ? are there any cons ?

1 Answers

In java, memory is allocated at run-time based on the class definition. It has nothing to do with the constructor that is used for initializing the object. Memory is allocated implicitly on the heap using the new operator.

Memory Allocation in Java - Refer the Overview section.

Related