Java this Keyword

Last Updated : 29 Sep, 2026

The this keyword in Java is a reference variable that refers to the current object of a class. It is commonly used to distinguish instance variables from local variables when they have the same name.

  • Refers to the current class object.
  • Used to access current class instance variables and methods.
  • Used to invoke another constructor of the same class using this().
Java
class Student {
    int id;
    String name;

    Student(int id, String name) {
        this.id = id;
        this.name = name;
    }

    void display() {
        System.out.println("ID: " + this.id);
        System.out.println("Name: " + this.name);
    }

    public static void main(String[] args) {
        Student s = new Student(101, "Geeks");
        s.display();
    }
}

Output
ID: 101
Name: Geeks

Explanation: Here, this.id and this.name refer to the instance variables of the current Student object, while id and name refer to the constructor parameters.

frame_4062

Uses of this Keyword in Java

1. To Refer to Current Class Instance Variables

The this keyword is commonly used when a constructor or method parameter has the same name as an instance variable.

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();
    }
}

Output
a = 10  b = 20

Explanation: Here, a and b on the right side refer to constructor parameters, while this.a and this.b refer to the current object's instance variables. Therefore, this removes the ambiguity between the parameters and instance variables.

2. To Invoke the Current Class Constructor

The this() syntax is used for constructor chaining, where one constructor calls another constructor of the same class.

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

The this keyword can be returned from an instance method to return the current object.

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();
    }
}

Output
a = 10  b = 20

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. To Pass the Current Object as a Method Argument

The this keyword can be passed as an argument to another method.

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();
    }
}

Output
a = 10  b = 20

Explanation: The get() method passes this to display(this). Thus, the current Geeks object is passed to the display() method, where its instance variables are accessed through obj.

5. To Invoke the Current Class Method

The this keyword can be used to explicitly invoke an instance method of the current object.

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

Explanation: 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. To Pass the Current Object as a Constructor Argument

The this keyword can be passed to another class's constructor when the current object needs to be provided as an argument.

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.

Comment