---
title: "What is this keyword in java?"  
description: "What is this keyword in java?"  
author: "Krishnapriya Rajeev"  
published: 2023-04-29  
updated: 2023-05-01  
canonical: https://www.mindstick.com/forum/158075/what-is-this-keyword-in-java  
category: "java"  
tags: ["java"]  
reading_time: 3 minutes  

---

# What is this keyword in java?

### What is this keyword in java?

## Replies

### Reply by Aryan Kumar

In Java, **this** is a keyword that refers to the current object or instance of a class. It is used to refer to the current object's instance variables, methods, and constructors.

Here are some common uses of the **this** keyword in Java:

- **To refer to instance variables:** When an instance variable has the same name as a parameter or local variable in a method, the **this** keyword can be used to distinguish between the two. For example:

```java
public class Person {
   private String name;
   public Person(String name) {
       this.name = name;
   }
   public void setName(String name) {
       this.name = name; // Use this.name to refer to the instance variable
   }
}
```

- **To call another constructor in the same class:** When a class has multiple constructors, one constructor can call another constructor using the **this** keyword. The called constructor must be the first statement in the calling constructor. For example:

```java
public class Person {
   private String name;
   private int age;
   public Person(String name) {
       this(name, 0); // Call the two-argument constructor with age = 0
   }
   public Person(String name, int age) {
       this.name = name;
       this.age = age;
   }
}
```

- **To return the current object from a method:** In some cases, a method needs to return the current object to allow method chaining. This can be done by returning **this**. For example:

```java
public class Person {
   private String name;
   private int age;
   public Person setName(String name) {
       this.name = name;
       return this; // Return the current object to allow method chaining
   }
   public Person setAge(int age) {
       this.age = age;
       return this; // Return the current object to allow method chaining
   }
}
```

The **this** keyword can also be used in other contexts, such as to pass the current object as a parameter to another method or to synchronize on the current object.

\

### Reply by Krishnapriya Rajeev

In Java, "this" is a keyword that refers to the current instance of the class in which it is used. It used in the following ways:

- In a method or constructor of a class, "this" can be used to refer to instance variables of the class. For example, "this.name" would refer to the "name" instance variable of the current instance of the class.
- A constructor can use "this" to invoke another constructor of the same class. This is useful when the same initialization code needs to be called from multiple constructors. The call to the other constructor must be the first statement in the constructor.
- In a method of a class, "this" can be used to return the current instance of the class. This is useful when chaining method calls or when returning the current object from a method.
- "this" can be used to pass the current instance of the class as a parameter to another method. This is useful when a method needs to access the state of the current instance of the class.

For example, consider the following class:

```plaintext
public class Person {
    private String name;

    public Person(String name) {
        this.name = name;
    }

    public String getName() {
        return this.name;
    }
}
```

In the constructor, "this.name" refers to the instance variable "name" of the current instance of the class. In the "getName" method, "this" is used to return the current instance of the class.


---

Original Source: https://www.mindstick.com/forum/158075/what-is-this-keyword-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
