Menu

Constructors in Java

Constructors in Java

In the previous chapter, you created a Car object and then set its fields one by one, colour, then speed. That works, but it is tedious and easy to forget a field. Constructors solve this by letting you set up an object's initial state the moment it is created.

What is a Constructor?

A constructor is a special block of code that runs automatically when an object is created with new. It looks like a method, but it has no return type, not even void, and its name must exactly match the class name.

public class Car {

String colour;

int speed;

// Constructor

Car(String c, int s) {

colour = c;

speed = s;

}

}

public class Main {

public static void main(String[] args) {

Car myCar = new Car("Red", 0);   // constructor runs here

System.out.println(myCar.colour); // Red

}

}

The moment new Car("Red", 0) executes, Java allocates memory for the object and immediately calls the constructor with those two arguments, setting colour and speed before the object is even handed back to you.

Default vs Parameterised Constructors

Java distinguishes between two broad kinds of constructors, based on whether they accept arguments:

  • constructor: takes no arguments. If you write no constructor at all, Java silently provides one for you, which sets all fields to their default values (0, false, null).
  • Parameterised constructor: takes one or more arguments, letting the caller supply specific initial values when the object is created.

public class Student {

String name;

int age;

// Default constructor (no arguments)

Student() {

name = "Unknown";

age = 0;

}

// Parameterized constructor

Student(String n, int a) {

name = n;

age = a;

}

}

Student s1 = new Student();           // name = Unknown, age = 0

Student s2 = new Student("Asha", 21); // name = Asha, age = 21

Did You Know?

Java only provides its automatic default constructor if you have written no constructors at all in the class. The moment you write even one parameterised constructor yourself, Java stops generating the free default one, and Student s1 = new Student() would no longer compile unless you write a no-argument constructor explicitly.

Constructor Overloading

A single class can have multiple constructors, as long as each one has a different parameter list (different number or types of parameters). This is called constructor overloading, and it gives callers flexibility in how they create an object, depending on what information they have available.

public class Student {

String name;

int age;

String course;

Student() {

this("Unknown", 0, "Not enrolled");   // calls the 3-arg constructor below

}

Student(String n, int a) {

this(n, a, "Not enrolled");

}

Student(String n, int a, String c) {

name = n;

age = a;

course = c;

}

}

Student s1 = new Student();

Student s2 = new Student("Ravi", 20);

Student s3 = new Student("Meera", 22, "Computer Science");

The this(...) call lets one constructor delegate to another in the same class, avoiding duplicated initialisation logic. It must be the very first statement inside the constructor if you use it.

Constructor Called

Arguments Provided

Result

Student()

none

name=Unknown, age=0, course=Not enrolled

Student(n, a)

2 arguments

name=n, age=a, course=Not enrolled

Student(n, a, c)

3 arguments

name=n, age=a, course=c