---
title: "Explain the Constructors in Java"  
description: "A 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."  
author: "Ashutosh Patel"  
published: 2025-03-21  
updated: 2025-03-21  
canonical: https://www.mindstick.com/articles/338835/explain-the-constructors-in-java  
category: "java"  
tags: ["java", "constructor", "java method"]  
reading_time: 4 minutes  

---

# Explain the Constructors in Java

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](https://yourviews.mindstick.com/view/85941/iphone-15-features-launch-date-and-is-it-worth-buying) 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](https://www.mindstick.com/forum/34631/instance-variables-and-class-variables).

## Types of Constructors in Java

- [Default Constructor](https://www.mindstick.com/forum/34531/default-constructor) (No-Argument Constructor)
- Parameterized Constructor
- [Copy Constructor](https://www.mindstick.com/forum/159685/define-copy-constructor-in-java) (User-defined)

## 1. Default Constructor (No-Argument Constructor)

The default constructor is one that does not take any [parameters](https://www.mindstick.com/articles/87/passing-parameters-in-c-sharp).

## Example

```java
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

```plaintext
Default Constructor Called!
```

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

## 2. Parameterized Constructor

Parameterized constructor allows [passing values](https://www.mindstick.com/forum/299/passing-values-from-one-view-to-another-view) ​​at the time of object [creation](https://yourviews.mindstick.com/view/85506/what-was-the-causes-of-creation-of-pakistan)

## Example

```java
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

```plaintext
Name: John, Age: 20
```

## 3. Copy Constructor (User-defined)

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

```java
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

```plaintext
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](https://www.mindstick.com/forum/33991/how-to-use-multiple-ng-app-in-a-single-page) constructors with different parameters.

```java
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

```plaintext
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

```java
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

```plaintext
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](https://answers.mindstick.com/qa/30493/how-to-get-duplicate-pan-card-reprint-pan-card) objects.
- The this() keyword can call another constructor within the same class.

Also, Read: [Abstraction and Interfaces in Java](https://www.mindstick.com/articles/338811/abstraction-and-interfaces-in-java)

---

Original Source: https://www.mindstick.com/articles/338835/explain-the-constructors-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
