> For the complete documentation index, see [llms.txt](https://www.pranaypourkar.co.in/the-programmers-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.pranaypourkar.co.in/the-programmers-guide/java/java-basics/java-keywords/this.md).

# this

In Java, the `this` keyword is a reference to the current instance of the class. It can be used inside a method or constructor to refer to the current object on which the method or constructor is being invoked.

## **Reference to Current Object**

When we use `this`, we're referring to the object on which the current method or constructor is being called.

### **Usage**:

* **Accessing instance variables**: We can use `this` to access instance variables of the current object, particularly when there's a naming conflict between instance variables and method parameters.

```java
class Person {
    private String name;
    private int age;

    public void setName(String name) {
        this.name = name; // "this" refers to the current Person object
    }
}
```

* **Invoking other constructors**: Within a constructor, `this` can be used to call other constructors of the same class. This is useful for constructor chaining.

```java
class Rectangle {
    private int width;
    private int height;

    // Default constructor
    public Rectangle() {
        this(1, 1); // Calling another constructor (constructor chaining)
    }

    // Parameterized constructor with width and height
    public Rectangle(int width, int height) {
        this.width = width;
        this.height = height;
    }
}
```

{% hint style="info" %}
In Java, when we are defining constructors, if we want to invoke another constructor of the same class, the call to another constructor must be the first statement in the constructor body.
{% endhint %}

* **Invoking methods**: We can use `this` to invoke instance methods.

```java
class Calculator {
    public void add(int a, int b) {
        int sum = a + b;
        System.out.println("Sum: " + sum);
    }

    public void subtract(int a, int b) {
        this.add(a, -b); // Calling the add() method from within subtract()
    }
}
```

* **Passing the current object**: We can pass `this` as an argument to other methods or constructors, allowing those methods or constructors to operate on the current object.

```java
class Rectangle {
    private int width;
    private int height;

    // Method to calculate and print the area
    public void printArea() {
        int area = this.width * this.height; // Accessing current object's width and height
        System.out.println("Area: " + area);
    }
}
```

* **Returning the Current Object:** We can return `this` from a method.

```java
class Point {
    private int x;
    private int y;

    public Point move(int dx, int dy) {
        this.x += dx;
        this.y += dy;
        return this; // Returns the modified Point object
    }
}
```

### **Scope**

The scope of `this` is limited to non-static contexts, such as instance methods and constructors. It cannot be used in **static** methods.

### **No Separate Allocation**

`this` itself does not have any memory allocation. It's simply a reference to the current object instance.

* **`this` Reference:** `this` keyword is a reference variable that refers to the current object instance. This reference variable itself is stored on the stack, typically within the method call frame where it's used.
* **Object Instance:** The object instance that `this` refers to is allocated on the heap memory. The heap is a more spacious memory area for storing objects and their data members.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://www.pranaypourkar.co.in/the-programmers-guide/java/java-basics/java-keywords/this.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
