JavaConstructors

Constructors

What Is a Constructor?

A constructor is a special block of code that runs automatically when an object is created with new. Its job is to initialize the new object's fields so it starts life in a valid state. A constructor looks like a method, but with two important differences: it has exactly the same name as the class, and it has no return type at all — not even void.

A basic constructor

Java
public class Point {

    int x;
    int y;

    // Constructor — same name as the class, no return type
    Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public static void main(String[] args) {
        Point p = new Point(3, 4); // the constructor runs here
        System.out.println(p.x + ", " + p.y);
    }
}
3, 4
Warning
A constructor must not declare a return type — not even void. If you write void Point(int x, int y) { ... }, you haven't written a constructor at all; you've written a regular method that happens to share the class's name. It won't run automatically when you call new Point(...), which is one of the most common early mistakes with constructors.
The Default Constructor

If you don't write any constructor at all, the compiler automatically supplies a default, no-argument constructor for you. It takes no parameters and does nothing beyond the default field initialization (0, false, null, and so on).

Relying on the compiler-provided default constructor

Java
public class Empty {
    int value; // defaults to 0
}

public class Main {
    public static void main(String[] args) {
        Empty e = new Empty(); // works — the compiler generated this constructor
        System.out.println(e.value);
    }
}
Warning
As soon as you define ANY constructor of your own — even a constructor that takes arguments — the compiler stops generating the default no-argument constructor. If you still need a no-argument way to create the object, you must write it yourself. This trips up a lot of learners:

class Empty { Empty(int v) { ... } }
new Empty(); // compile error — no matching constructor
Constructor Overloading

Just like regular methods, a class can define multiple constructors as long as their parameter lists differ (in number, order, or type). This is called constructor overloading, and it lets callers create objects in whichever way is most convenient for them.

Multiple constructors for the same class

Java
public class Rectangle {

    double width;
    double height;

    Rectangle(double width, double height) {
        this.width = width;
        this.height = height;
    }

    Rectangle(double side) { // a square is a rectangle with equal sides
        this.width = side;
        this.height = side;
    }

    Rectangle() { // default size
        this.width = 1;
        this.height = 1;
    }

    double area() {
        return width * height;
    }
}
Constructor Chaining with this(...)

When several constructors share setup logic, you can have one constructor call another constructor in the same class using this(...). This avoids duplicating initialization code, and the call to this(...) must be the very first statement in the constructor.

Chaining constructors with this(...)

Java
public class Rectangle {

    double width;
    double height;

    Rectangle(double width, double height) {
        this.width = width;
        this.height = height;
    }

    Rectangle(double side) {
        this(side, side); // delegates to the two-argument constructor above
    }

    Rectangle() {
        this(1, 1); // delegates to the two-argument constructor above
    }

    double area() {
        return width * height;
    }

    public static void main(String[] args) {
        Rectangle r1 = new Rectangle(4, 5);
        Rectangle r2 = new Rectangle(3);   // a 3x3 square
        Rectangle r3 = new Rectangle();    // a 1x1 default

        System.out.println(r1.area());
        System.out.println(r2.area());
        System.out.println(r3.area());
    }
}
20.0
9.0
1.0
Note
Constructor chaining with this(...) is different from super(...), which calls a constructor in the PARENT class instead of the same class. Both must be the first statement in a constructor, and both are covered in more depth — this(...) on the this-keyword page, and super(...) on the super-keyword page.
  • A constructor shares the class's name exactly and never declares a return type

  • The compiler only provides a free no-argument constructor if you define no constructors yourself

  • Overload constructors to offer convenient ways of building an object

  • Use this(...) to funnel overloaded constructors through one piece of shared initialization logic