Java Documentation

1.5 Java Constructors

How Java uses special methods called constructors to initialize objects, including default constructors, parameterized constructors, and constructor overloading.

What makes a constructor special?

A constructor is a special method that runs automatically when you create an object. Its main job is to initialize the fields of that new object.

  • A constructor has no return type (not evenvoid).
  • Its name must be exactly the same as the class name.
  • If you do not write any constructor, Java provides adefault constructor that sets reference fields to null and numeric fields to0.

Types of constructors in Java

1. Default (no-argument) constructor

A default constructor has no parameters. It is often used to assign common or fallback values to every new object.

defaultConstructor.java
class Car {
    private String model;
    private String color;

    // Constructor
    Car() {
        model = "BMW";
        color = "BLUE";
    }

    public void showInfo() {
        System.out.println("Model: " + model + " Color: " + color);
    }
}
Reference & full codeView on GitHub

2. Parameterized constructor

A parameterized constructor takes arguments so that you can pass different values when creating each object.

parameterizedConstructor.java
class Car {
    private String model;
    private String color;

    // Parameterized constructor
    Car(String m, String c) {
        model = m;
        color = c;
    }
}

// Usage
Car car1 = new Car("Creta", "Red");
Reference & full codeView on GitHub

3. Constructor overloading

Just like methods, constructors can be overloaded. You can define multiple constructors with different parameter lists, and Java will choose the right one based on how you create the object.

constructorOverloading.java
class Student {
    String name;
    int roll;

    Student() { /* Default */ }

    Student(String n, int r) { /* Two parameters */ }

    Student(String n, int r, String faculty) { /* Three parameters */ }
}
Reference & full codeView on GitHub

All-in-one constructor example

The following program combines default, parameterized, and overloaded constructors in a single car class and demonstrates how each one is called from main.

Constructor.java
class car {
    private String model;
    private String color;
    private int model_no;

    car() {
        model = "BMW";
        color = "Red";
    }

    car(String m, String c) {
        model = m;
        color = c;
    }

    car(String m, String c, int mn) {
        model = m;
        color = c;
        model_no = mn;
    }

    public void showInfo() {
        System.out.println("Model: " + model);
        System.out.println("Color: " + color);
        System.out.println("Model No: " + model_no);
    }
}

public class Constructor {
    public static void main(String[] args) {

        car car1 = new car();
        // Default constructor called
        car car2 = new car("Lambo", "Black");
        // Parameterized constructor called
        car car3 = new car("Supra", "Black", 10);

        car1.showInfo();
        car2.showInfo();
        car3.showInfo();
    }
}
Reference & full codeView on GitHub

Output

When you run the above program, the console will display something like this:

Model: BMW
Color: Red
Model No: 0
Model: Lambo
Color: Black
Model No: 0
Model: Supra
Color: Black
Model No: 10