articles

home / developersection / articles / explain the constructors in java

Explain the Constructors in Java

Explain the Constructors in Java

Ashutosh Kumar Verma 486 21-Mar-2025

Constructor in Java is a special type of method that is used to initialize an object. It is called automatically when an object is created.

 

Features of a Constructor

  • It has the same name as the class.
  • It has no return type (not even void).
  • It is automatically invoked when an object is created.
  • It is used to initialize instance variables.

 

Types of Constructors in Java

  • Default Constructor (No-Argument Constructor)
  • Parameterized Constructor
  • Copy Constructor (User-defined)

 

1. Default Constructor (No-Argument Constructor)

The default constructor is one that does not take any parameters.

Example

class Student {
   // Default constructor
   Student() {
       System.out.println("Default Constructor Called!");
   }
   public static void main(String[] args) {
       Student obj = new Student(); // Constructor automatically called
   }
}

Output

Default Constructor Called!

If there is no constructor is defined, Java indirectly provides a default constructor.

 

2. Parameterized Constructor

Parameterized constructor allows passing values ​​at the time of object creation

Example

class Student {
   String name;
   int age;
   // Parameterized Constructor
   Student(String n, int a) {
       name = n;
       age = a;
   }
   void display() {
       System.out.println("Name: " + name + ", Age: " + age);
   }
   public static void main(String[] args) {
       Student obj = new Student("John", 20); // Passing values
       obj.display();
   }
}

Output

Name: John, Age: 20

 

3. Copy Constructor (User-defined)

The copy constructor creates a new object by copying the values ​​from another object.

class Student {
   String name;
   int age;
   // Parameterized Constructor
   Student(String n, int a) {
       name = n;
       age = a;
   }
   // Copy Constructor
   Student(Student s) {
       name = s.name;
       age = s.age;
   }
   void display() {
       System.out.println("Name: " + name + ", Age: " + age);
   }
   public static void main(String[] args) {
       Student s1 = new Student("Alice", 22);
       Student s2 = new Student(s1); // Copying s1 into s2
       s1.display();
       s2.display();
   }
}

Output

Name: Alice, Age: 22
Name: Alice, Age: 22

The copy constructor is not built-in in Java. We have to define it manually.

 

Constructor Overloading

A class can have multiple constructors with different parameters.

class Person {
   String name;
   int age;
   // Default Constructor
   Person() {
       name = "Unknown";
       age = 0;
   }
   // Parameterized Constructor
   Person(String n, int a) {
       name = n;
       age = a;
   }
   void display() {
       System.out.println("Name: " + name + ", Age: " + age);
   }
   public static void main(String[] args) {
       Person p1 = new Person();          // Calls default constructor
       Person p2 = new Person("Bob", 25); // Calls parameterized constructor
       p1.display();
       p2.display();
   }
}

Output

Name: Unknown, Age: 0
Name: Bob, Age: 25

 

Using this() to Call Another Constructor

In Java, the this() keyword is used to call another constructor within the same class.

Example

class Student {
   String name;
   int age;
   // Constructor with two parameters
   Student(String n, int a) {
       name = n;
       age = a;
   }
   // Constructor with one parameter
   Student(String n) {
       this(n, 18); // Calls the constructor with two parameters
   }
   void display() {
       System.out.println("Name: " + name + ", Age: " + age);
   }
   public static void main(String[] args) {
       Student s1 = new Student("David");
       s1.display();
   }
}

Ouput

Name: David, Age: 18

Summary

  • Constructors help to initialize the object automatically.
  • They do not have any return type.
  • Java provides a default constructor if one is not defined.
  • We can overload the constructor to provide flexibility.
  • Copy constructors help to create duplicate objects.
  • The this() keyword can call another constructor within the same class.

 

Also, Read: Abstraction and Interfaces in Java


Updated 21-Mar-2025

Hi! This is Ashutosh Kumar Verma. I am a software developer at MindStick Software Pvt Ltd since 2021. I have added some new and interesting features to the MindStick website like a story section, audio section, and merge profile feature on MindStick subdomains, etc. I love coding and I have good knowledge of SQL Database.

Leave Comment

Comments

Liked By