Java Constructors
A constructor is a special method that is automatically called when an object is created. Its main purpose is to initialize the fields of an object at the time of creation. Without a constructor, fields would hold default values and developers would have to set them manually every time an object is created.
Key Characteristics of Constructors
- The constructor name must exactly match the class name.
- A constructor has no return type — not even
void. - It is called automatically when an object is created using the
newkeyword. - A class can have multiple constructors (constructor overloading).
Types of Constructors
1. Default Constructor (No-Argument Constructor)
A default constructor takes no parameters. If no constructor is defined in a class, Java automatically provides a hidden default constructor. If defined explicitly, it initializes fields with custom default values.
class Laptop {
String brand;
int ram;
// Default constructor
Laptop() {
brand = "Unknown";
ram = 8;
}
void display() {
System.out.println("Brand: " + brand + ", RAM: " + ram + "GB");
}
}
public class Main {
public static void main(String[] args) {
Laptop l = new Laptop(); // default constructor is called
l.display();
}
}Output:
Brand: Unknown, RAM: 8GB2. Parameterized Constructor
A parameterized constructor accepts arguments, allowing each object to be initialized with unique values at the time of creation.
class Laptop {
String brand;
int ram;
// Parameterized constructor
Laptop(String brand, int ram) {
this.brand = brand;
this.ram = ram;
}
void display() {
System.out.println("Brand: " + brand + ", RAM: " + ram + "GB");
}
}
public class Main {
public static void main(String[] args) {
Laptop l1 = new Laptop("Dell", 16);
Laptop l2 = new Laptop("HP", 8);
l1.display();
l2.display();
}
}Output:
Brand: Dell, RAM: 16GB
Brand: HP, RAM: 8GBThe keyword this is used to distinguish between the constructor parameter (brand) and the class field (this.brand). This is covered in detail in the next topic.
3. Constructor Overloading
Just like method overloading, a class can have multiple constructors with different parameter lists. Java calls the correct constructor based on the arguments passed.
class Rectangle {
double length;
double width;
// Constructor 1: No arguments – creates a square of side 1
Rectangle() {
length = 1;
width = 1;
}
// Constructor 2: One argument – creates a square
Rectangle(double side) {
length = side;
width = side;
}
// Constructor 3: Two arguments – creates a rectangle
Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
double area() {
return length * width;
}
}
public class Main {
public static void main(String[] args) {
Rectangle r1 = new Rectangle();
Rectangle r2 = new Rectangle(5);
Rectangle r3 = new Rectangle(4, 7);
System.out.println("Area 1: " + r1.area()); // 1.0
System.out.println("Area 2: " + r2.area()); // 25.0
System.out.println("Area 3: " + r3.area()); // 28.0
}
}Constructor Chaining – Using this()
One constructor can call another constructor in the same class using this(). This avoids code duplication and is called constructor chaining. The this() call must be the first statement in the constructor body.
class Phone {
String brand;
String model;
int battery;
Phone() {
this("Generic", "Basic", 3000); // calls the 3-parameter constructor
}
Phone(String brand, String model) {
this(brand, model, 4000); // calls the 3-parameter constructor
}
Phone(String brand, String model, int battery) {
this.brand = brand;
this.model = model;
this.battery = battery;
}
void display() {
System.out.println(brand + " | " + model + " | " + battery + "mAh");
}
}
public class Main {
public static void main(String[] args) {
Phone p1 = new Phone();
Phone p2 = new Phone("Samsung", "Galaxy S");
Phone p3 = new Phone("Apple", "iPhone 14", 3279);
p1.display();
p2.display();
p3.display();
}
}Output:
Generic | Basic | 3000mAh
Samsung | Galaxy S | 4000mAh
Apple | iPhone 14 | 3279mAhThe Default Constructor Provided by Java
If a class has no constructor defined at all, Java automatically inserts a no-argument default constructor behind the scenes. However, as soon as any constructor is defined explicitly, Java no longer provides the automatic default constructor.
class Animal {
String name;
// No constructor written – Java provides a default one
}
Animal a = new Animal(); // works fine
a.name = "Lion";But if a parameterized constructor is added and no no-arg constructor is written, calling new Animal() without arguments will cause a compile error.
Constructor vs Method
| Feature | Constructor | Method |
|---|---|---|
| Name | Same as class name | Any valid identifier |
| Return type | None (not even void) | Must have a return type (or void) |
| Called when | Object is created (automatically) | Explicitly called in code |
| Purpose | Initialize an object | Perform an operation |
| Overloading | Yes | Yes |
Summary
- A constructor initializes an object's fields when it is created.
- It has the same name as the class and no return type.
- A default constructor takes no arguments; a parameterized constructor accepts input values.
- Constructor overloading allows multiple constructors with different parameters in the same class.
- Use
this()to call one constructor from another within the same class. - If no constructor is defined, Java provides a default one automatically.
