Skip to main content
Java Basics
CHAPTER 14 Beginner

Constructors in Java

Updated: May 17, 2026
5 min read

# CHAPTER 14

Constructors in Java

1. Introduction

Constructors are special methods that initialize objects the moment they are created. Instead of setting each field manually after creating an object, a constructor lets you set everything in one shot during instantiation.

2. Learning Objectives

  • Understand what constructors are and why they're needed.
  • Create default and parameterized constructors.
  • Implement constructor overloading.
  • Use this() for constructor chaining.
  • Understand the copy constructor pattern.

3. What is a Constructor?

A constructor is a special method with the same name as the class and no return type (not even void).
java
123456789101112
public class Car {
    String model;
    int year;
    
    // Constructor
    Car(String model, int year) {
        this.model = model;
        this.year = year;
    }
}

Car myCar = new Car("Tesla", 2024); // Constructor called automatically

4. Default Constructor

If you don't write any constructor, Java provides a default no-argument constructor automatically.
java
123456
public class Book {
    String title;
    // Java provides: Book() {} implicitly
}

Book b = new Book(); // Uses default constructor

Important: If you write ANY constructor, Java no longer provides the default one!

5. Parameterized Constructor

java
123456789101112131415161718
public class Student {
    String name;
    int age;
    
    Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    void display() {
        System.out.println(name + " - " + age);
    }
    
    public static void main(String[] args) {
        Student s = new Student("Alice", 20);
        s.display(); // Alice - 20
    }
}

6. Constructor Overloading

Multiple constructors with different parameter lists:
java
1234567891011121314151617181920
public class Rectangle {
    double length, width;
    
    Rectangle() {
        this.length = 1.0;
        this.width = 1.0;
    }
    
    Rectangle(double side) {
        this.length = side;
        this.width = side;  // Square
    }
    
    Rectangle(double length, double width) {
        this.length = length;
        this.width = width;
    }
    
    double area() { return length * width; }
}

7. Constructor Chaining with this()

One constructor calling another:
java
12345678910111213141516171819
public class Employee {
    String name;
    String dept;
    double salary;
    
    Employee() {
        this("Unknown", "Unassigned", 0.0); // Calls 3-param constructor
    }
    
    Employee(String name) {
        this(name, "General", 50000.0);
    }
    
    Employee(String name, String dept, double salary) {
        this.name = name;
        this.dept = dept;
        this.salary = salary;
    }
}

8. Copy Constructor

java
1234567891011121314
public class Point {
    int x, y;
    
    Point(int x, int y) { this.x = x; this.y = y; }
    
    // Copy constructor
    Point(Point other) {
        this.x = other.x;
        this.y = other.y;
    }
}

Point p1 = new Point(10, 20);
Point p2 = new Point(p1); // Copy of p1

9. Common Mistakes

  • Adding a return type to a constructor: void Car() is a method, not a constructor!
  • Forgetting to call this.field: Without this, the parameter shadows the field.
  • Not providing a no-arg constructor when needed: Some frameworks (like Hibernate) require one.

10. MCQ Quiz with Answers

Question 1

A constructor has the same name as:

Question 2

Constructors have a return type of:

Question 3

What is constructor overloading?

Question 4

this() calls:

Question 5

When is the default constructor NOT provided?

Question 6

A copy constructor takes:

Question 7

Constructors are called when?

Question 8

Can constructors be private?

Question 9

this in a constructor refers to:

Question 10

Can constructors be inherited?

11. Interview Questions

  • Q: What is the difference between a constructor and a method?
  • Q: What is constructor chaining?
  • Q: Can a constructor call another constructor? How?
  • Q: Why would you make a constructor private?

12. Summary

Constructors initialize objects at creation time. Java provides a default no-arg constructor unless you define your own. Constructor overloading allows flexible initialization. this() enables constructor chaining. Copy constructors create independent duplicates of objects.

13. Next Chapter Recommendation

In Chapter 15: Inheritance in Java, we'll learn how classes can inherit from parent classes, promoting code reuse and establishing hierarchies.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·