---
title: "How to call one constructor from another?"  
description: "How to call one constructor from another?"  
author: "Steilla Mitchel"  
published: 2023-08-18  
updated: 2023-08-19  
canonical: https://www.mindstick.com/forum/159570/how-to-call-one-constructor-from-another  
category: "c#"  
tags: ["c#", "constructor"]  
reading_time: 2 minutes  

---

# How to call one constructor from another?

How to call [one constructor](https://www.mindstick.com/forum/159223/how-can-i-call-one-constructor-from-another-in-java) from another?

## Replies

### Reply by Aryan Kumar

In Java, you can call one constructor from another using the `this()` keyword. The `this()` keyword refers to the current object.

To call one constructor from another, you need to use the `this()` keyword followed by the name of the constructor you want to call. The constructor you want to call must be defined in the same class as the constructor that is calling it.

Here is an example of how to call one constructor from another in Java:

Java

```plaintext
class Person {
    public Person() {
        // This is the default constructor.
    }

    public Person(String name) {
        // This constructor takes a name parameter.
        this(); // Call the default constructor first.
        this.name = name;
    }

    private String name;
}
```

In this example, the `Person()` constructor is the default constructor. The `Person(String name)` constructor takes a name parameter. The first line of the `Person(String name)` constructor calls the `Person()` constructor first. This is done to ensure that the fields of the `Person` class are initialized before the name property is set.

Here is another example:

Java

```plaintext
class Person {
    public Person(String name, int age) {
        // This constructor takes a name and age parameter.
        this(name); // Call the constructor that takes a name parameter first.
        this.age = age;
    }

    public Person(String name) {
        // This constructor takes a name parameter.
        this.name = name;
    }

    private String name;
    private int age;
}
```

In this example, the `Person(String name, int age)` constructor calls the `Person(String name)` constructor first. This is done to ensure that the name property is initialized before the age property is set.


---

Original Source: https://www.mindstick.com/forum/159570/how-to-call-one-constructor-from-another

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
