Constructor this()
constructor this()
constructor call other constructor in same class.
it must be in first line of constructor.
class Car {
String color;
String gearType;
int door;
Car() {//default constructor
color = "white";
gearType = "auto";
door = 4;
}
Car(String c, String g, int d) {
color = c;
gearType = g;
door = d;
}
}
Remove duplicated codes :
class Car {
String color;
String gearType;
int door;
Car() {//default constructor
//Car("white","auto",4);
this("white","auto",4);
}
Car(String c, String g, int d) {
color = c;
gearType = g;
door = d;
}
}
Last updated
Was this helpful?