A constructor is a special method that runs when you create an object with new. It has the same name as the class and no return type:
class Car {
String color;
int speed;
Car(String color, int speed) {
this.color = color;
this.speed = speed;
}
}
Now you create a car like this: Car myCar = new Car("red", 60);. The constructor sets both fields immediately. Without a constructor, you'd have to set each field manually after calling new, which is error-prone. You might forget one and end up with a half-initialized object.