Constructor

A constructor is a special method that Java calls automatically when you create an object with new. It has the same name as the class and no return type. Its job is to initialise the object's fields.

Example

public class Person {
    private final String name;
    private final int age;

    public Person(String name, int age) {  // constructor
        this.name = name;
        this.age  = age;
    }
}

Person p = new Person("Ada", 36);

Default constructor

If you declare no constructors at all, Java adds an implicit no-argument constructor that does nothing. The moment you declare any constructor, the default disappears.

Constructor chaining with this() and super()

public Product(String name) {
    this(name, 0.0);  // delegates to the 2-arg constructor
}
public Product(String name, double price) {
    super();          // must be first if present — calls the parent's constructor
    this.name  = name;
    this.price = price;
}

See the full constructor guide for inheritance rules, initialisation order and immutability patterns.