A constructor is a special method in Java that gets called whenever you create a new object from a class — its whole job is to set that object up and ready to go.
Every Java class needs a constructor, but you don't always have to write one yourself. If you skip it, the JVM quietly creates a default one behind the scenes.
A default constructor is simply one that doesn't take any parameters — it's there just to let you create the object, without necessarily setting any specific values right away.
Here's what that looks like in practice:
public class Person {
private int age;
private String name;
private String city;
// default constructor
public Person() {
}
// constructor with parameters
public Person(int age, String name, String city) {
this.age = age;
this.name = name;
this.city = city;
}
}
With a class like this, you've got options. You can create an object using the default constructor and fill in the details later with getters and setters, or you can use the second constructor to pass everything in right away when the object is created.