DEV Community

Cover image for Constructor in Java
Vigneshwaran V
Vigneshwaran V

Posted on

Constructor in Java

What Is Constructor

A constructor in Java is a special method that is automatically called when an object of a class is created. Its main purpose is to initialize the object's data.

Rules:

  • Constructor name must be the same as the class name.

  • It does not have a return type (not even void).

  • It is called automatically when you create an object using the new keyword.

  • A class can have multiple constructors (constructor overloading).

Example:

class Student {

    String name;
    int age;

    Student() {
        name = "Vignesh";
        age = 22;
    }

    void display() {
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
    }

    public static void main(String[] args) {

        Student s1 = new Student();

        s1.display();
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Name: Vignesh
Age: 22
Enter fullscreen mode Exit fullscreen mode

When we write Student s1 = new Student(); the constructor Student() is automatically called.

So the flow is

new Student() => Student() constructor => name = "Vignesh"
age = 22 => s1 object is initialized

Enter fullscreen mode Exit fullscreen mode

Constructor Overloading

Constructor overloading means having multiple constructors in the same class with different parameter lists.

For example

class Student {

    String name;
    int age;

    // Constructor 1
    Student() {
        name = "Unknown";
        age = 0;
    }

    // Constructor 2
    Student(String n) {
        name = n;
        age = 0;
    }

    // Constructor 3
    Student(String n, int a) {
        name = n;
        age = a;
    }

    void display() {
        System.out.println(name + " " + age);
    }

    public static void main(String[] args) {

        Student s1 = new Student();
        Student s2 = new Student("Vignesh");
        Student s3 = new Student("Arun", 21);

        s1.display();
        s2.display();
        s3.display();
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

Unknown 0
Vignesh 0
Arun 21
Enter fullscreen mode Exit fullscreen mode

Here we have three constructors

Student()
Student(String n)
Student(String n, int a)
Enter fullscreen mode Exit fullscreen mode

They have the same name, but their parameter lists are different,

That's constructor overloading.

this Keyword

  • Once you understand constructors, this becomes much easier.

  • The this keyword refers to the current object.

  • The most common use is when the instance variable and constructor parameter have the same name.

Without this

class Student {

    String name;
    int age;

    Student(String name, int age) {

        name = name;
        age = age;
    }
}
Enter fullscreen mode Exit fullscreen mode

This is a problem because Java cannot distinguish the instance variable from the parameter in the way we intend. Both sides refer to the parameter.

So we use this

this with Constructor

class Student {

    String name;
    int age;

    Student(String name, int age) {

        this.name = name;
        this.age = age;
    }

    void display() {
        System.out.println(this.name);
        System.out.println(this.age);
    }

    public static void main(String[] args) {

        Student s1 = new Student("Vignesh", 22);

        s1.display();
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

Vignesh
22
Enter fullscreen mode Exit fullscreen mode

Understand this line

this.name = name;
Enter fullscreen mode Exit fullscreen mode

There are two names.

this.name       =       name
                          
instance variable       parameter
of current object
Enter fullscreen mode Exit fullscreen mode

So here

this.name = name;
Enter fullscreen mode Exit fullscreen mode

Store the parameter name into the current object's name variable.

this Means Current Object

Consider these Two Objects

Student s1 = new Student("Vignesh", 22);
Student s2 = new Student("Arun", 21);
Enter fullscreen mode Exit fullscreen mode

For s1:

this → s1
Enter fullscreen mode Exit fullscreen mode

For s2:

this → s2
Enter fullscreen mode Exit fullscreen mode

So Therefore here:

this.name
Enter fullscreen mode Exit fullscreen mode

name of the current object


this is a reference variable in Java that refers to the current object. It is commonly used to distinguish instance variables from parameters

References

https://www.geeksforgeeks.org/java/constructors-in-java/
https://www.w3schools.com/java/java_constructors.asp
https://www.programiz.com/java-programming/constructors

Top comments (0)