Course Content
Core Java
About Lesson

In Java, the this keyword is a reference variable that refers to the current instance of the class. It can be used within the class to refer to the current object’s instance variables, methods, or constructors. Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called.

It is applicable only within non-static contexts, such as instance methods, constructors, or instance initialization blocks.

It is primarily used to eliminate ambiguity between instance variables and parameters or local variables with the same name, as well as to invoke instance methods and constructors.

Usage of this

  1. Ambiguity Resolution: When a local variable or parameter has the same name as an instance variable, this can be used to refer to the instance variable explicitly.
public class MyClass {
    private int x;

    public void setX(int x) {
        this.x = x; // Refers to the instance variable x
    }
}
  1. Calling Other Methods: this can be used to call other instance methods of the same class from within another method.
public class MyClass {
    public void method1() {
        System.out.println("Inside method1");
    }

    public void method2() {
        this.method1(); // Calling method1 using this
    }
}
  1. Using this with a Constructor: From within a constructor, you can also use the this keyword to call another constructor in the same class. Doing so is called an explicit constructor invocation or constructor chaining.
public class MyClass {
    private int x;

    public MyClass() {
        this(0); // Invokes parameterized constructor with default value
    }

    public MyClass(int x) {
        this.x = x;
    }
}
  1. Passing Object Reference: this can be passed as an argument to methods or constructors to pass a reference to the current object.
public class MyClass {
    public void method(MyClass obj) {
        // Method implementation
    }

    public void callMethod() {
        method(this); // Passing current object reference
    }
}
  1. this can be used to return current class instance. this can be returned as statement from the method.
public class Person {
    private String name;
    private int age;

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

    // Setter for name that returns the current instance
    public Person setName(String name) {
        this.name = name;
        return this; // Returning the current instance
    }

    // Setter for age that returns the current instance
    public Person setAge(int age) {
        this.age = age;
        return this; // Returning the current instance
    }

    // Method to print Person details
    public void printPerson() {
        System.out.println("Name: " + name + ", Age: " + age);
    }

    public static void main(String[] args) {
        // Creating a new Person instance and using method chaining
        Person person = new Person("Alice", 25)
                            .setName("Bob")
                            .setAge(30);
        
        // Printing the updated Person details
        person.printPerson();
    }
}

Scroll to Top