Java this Keyword (original) (raw)

Last Updated : 7 Mar, 2026

In Java, this is a keyword that refers to the current object, the object whose method or constructor is being executed. It is mainly used to:

**Note: There is no concept of “transitivity” or “non-transitivity” — this always refers to the current object of a class.

Java `

public class Person {

// Fields Declared
String name;
int age;

// Constructor
Person(String name, int age)
{
    this.name = name;
    this.age = age;
}

// Getter for name
public String get_name() { return name; }

// Setter for name
public void change_name(String name)
{
    this.name = name;
}

// Method to Print the Details of the person
public void printDetails()
{
    System.out.println("Name: " + this.name);
    System.out.println("Age: " + this.age);
    System.out.println();
}

// main function
public static void main(String[] args)
{
    // Objects Declared
    Person first = new Person("ABC", 18);
    Person second = new Person("XYZ", 22);

    first.printDetails();
    second.printDetails();

    first.change_name("PQR");
    System.out.println("Name has been changed to: "
                       + first.get_name());
}

}

`

Output

Name: ABC Age: 18

Name: XYZ Age: 22

Name has been changed to: PQR

**Explanation:

Methods to Use "this" in Java

1. To Refer Current Class Instance Variables

Java `

class Geeks { int a; int b;

// Parameterized constructor
Geeks(int a, int b)
{
    this.a = a;
    this.b = b;
}

void display()
{
    // Displaying value of variables a and b
    System.out.println("a = " + a + "  b = " + b);
}

public static void main(String[] args)
{
    Geeks object = new Geeks(10, 20);
    object.display();
}

}

`

**Explanation: The constructor parameters a and b have the same names as the instance variables, so **this.a and **this.b are used to refer to the current object’s variables and assign them the passed values. Without this, Java would only refer to the constructor parameters, leaving the instance variables uninitialized.

2. To Invoke Current Class Constructor

Java `

class Geeks { int a; int b;

// Default constructor
Geeks()
{
    this(10, 20);
    System.out.println(
        "Inside  default constructor \n");
}

// Parameterized constructor
Geeks(int a, int b)
{
    this.a = a;
    this.b = b;
    System.out.println(
        "Inside parameterized constructor");
}

public static void main(String[] args)
{
    Geeks object = new Geeks();
}

}

`

Output

Inside parameterized constructor Inside default constructor

**Explanation: The default constructor Geeks() calls the parameterized constructor using this(10, 20), so the parameterized constructor executes first. After initializing a and b, control returns to the default constructor, which then prints its own message.

3. To Return the Current Class Instance

Java `

class Geeks { int a; int b;

// Default constructor
Geeks()
{
    a = 10;
    b = 20;
}

// Method that returns current class instance
Geeks get() { 
    return this; 
}

// Displaying value of variables a and b
void display()
{
    System.out.println("a = " + a + "  b = " + b);
}

public static void main(String[] args)
{
    Geeks object = new Geeks();
    object.get().display();
}

}

`

**Explanation: The method get() returns the current object using this, so object.get() refers to the same Geeks instance created in main. Calling display() on it prints the values of a and b that were initialized in the default constructor.

4. As a Method Parameter

Java `

class Geeks { int a; int b;

// Default constructor
Geeks()
{
    a = 10;
    b = 20;
}

// Method that receives "this"
// keyword as parameter
void display(Geeks obj)
{
    System.out.println("a = " + obj.a
                       + "  b = " + obj.b);
}

// Method that returns current class instance
void get() { 
    display(this); 
}

// main function
public static void main(String[] args)
{
    Geeks object = new Geeks();
    object.get();
}

}

`

**Explanation: In the get() method, this is passed to display(this), so the current Geeks object is sent as a parameter. The display(Geeks obj) method then accesses obj.a and obj.b, which are the same values initialized in the constructor.

5. To Invoke the Current Class Method

Java `

class Geeks {

void display()
{
    // calling function show()
    this.show();

    System.out.println("Inside display function");
}

void show()
{
    System.out.println("Inside show function");
}

public static void main(String args[])
{
    Geeks g1 = new Geeks();
    g1.display();
}

}

`

Output

Inside show function Inside display function

**Example: Inside display(), this.show() calls the show() method of the same Geeks object before executing the next statement. After show() prints its message, control returns to display(), which then prints "Inside display function".

6. As an Argument in the Constructor Call

Java `

class A { B obj;

// Parameterized constructor with object of B
// as a parameter
A(B obj)
{
    this.obj = obj;

    // calling display method of class B
    obj.display();
}

}

class B { int x = 5;

// Default Constructor that create an object of A
// with passing this as an argument in the
// constructor
B() { A obj = new A(this); }

// method to show value of x
void display()
{
    System.out.println("Value of x in Class B : " + x);
}

public static void main(String[] args)
{
    B obj = new B();
}

}

`

Output

Value of x in Class B : 5

**Explanation: In class B, the constructor passes this to new A(this), sending the current B object to class A. Class A stores this reference and calls obj.display(), which prints the value of x from the same B instance.

Advantages of Using "this" Reference

There are many advantages of using "this" reference in Java as mentioned below:

Disadvantages of Using "this" Reference

Although "this" reference comes with many advantages there are disadvantages of also: