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

Harry :

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 ?

gany_15 :

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=322503&siteId=1