DEV Community

Cover image for Day 12: Understanding Constructors in Java
Karthick Narayanan
Karthick Narayanan

Posted on

Day 12: Understanding Constructors in Java

What is a Constructor?

A constructor is a special method in Java that is automatically called when an object is created.

Key Characteristics of a Constructor

  • Constructor name must be the same as the class name
  • It does not have a return type
  • It is called automatically when an object is created
  • Used to initialize instance variables

Why Do We Need Constructors?

Constructors help to:

  • Initialize object values
  • Reduce extra setter methods
  • Ensure objects are created in a valid state
  • Improve code readability and structure

Syntax of a Constructor

class Student {
    Student() {
        System.out.println("Constructor called");
    }
}
Enter fullscreen mode Exit fullscreen mode

When an object of Student is created, the constructor executes automatically.


Types of Constructors in Java

Java mainly has two types of constructors:

  1. Default Constructor
  2. Parameterized Constructor

1. Default Constructor

A default constructor is a constructor with no parameters.

Example:

class Employee {

    Employee() {
        System.out.println("Hi");
    }

    public static void main(String[] args) {
        Employee emp = new Employee();
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation:

When the object emp is created, the default constructor is called and prints Hi.


2. Parameterized Constructor

A parameterized constructor accepts parameters to initialize object variables.

Example:

class Employee {

    int empId;
    String empName;
    int empAge;

    Employee(int empId, int empAge, String empName) {
        this.empId = empId;
        this.empAge = empAge;
        this.empName = empName;
    }

    public static void main(String[] args) {
        Employee kumar = new Employee(101, 20, "Kumar");
        System.out.println(kumar.empId);
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. We have created a class named Employee.

  2. Inside the class, we declared three variables:
    int empId;, String empName;, and int empAge;.
    These variables are declared but not initialized.
    Since they are declared inside the class and outside the main method, they are instance (global) variables, which can be accessed anywhere inside the class using an object.

  3. We then created a parameterized constructor named Employee that accepts three arguments with their respective data types.
    Inside the constructor, we assigned the local variables (constructor parameters) to the instance variables using the this keyword.

  4. In the main method, we created an object named kumar using the object creation syntax.
    While creating the object, we passed values directly inside the constructor arguments.
    These values are received by the constructor parameters and then assigned to the instance variables using the this keyword.

  5. Since the values are now stored in the instance variables, we can access them using the object reference name.
    Here, we printed the employee ID using:

   System.out.println(kumar.empId);
Enter fullscreen mode Exit fullscreen mode

This prints the output:

   101
Enter fullscreen mode Exit fullscreen mode

Summary

A parameterized constructor allows us to initialize object values at the time of object creation using arguments.


The this Keyword in Constructors

What is this?

this is a reference variable that refers to the current object.


Why Use this Keyword?

When constructor parameters and instance variables have the same names, Java gives priority to the parameters.
The this keyword is used to differentiate instance variables from parameters.


Example Explanation

this.empId = empId;
Enter fullscreen mode Exit fullscreen mode
  • this.empId → instance variable of the current object
  • empId → constructor parameter

This ensures the passed value is stored in the object.


Top comments (0)