Lesson 35 of 50 – Java this Keyword
70%

Java this Keyword

In Java, the this keyword refers to the current object. It is commonly used to access instance variables, call methods, and invoke another constructor in the same class.

1. What Is the this Keyword?

The this keyword represents the current object whose instance method or constructor is executing.

class Student {
    String name = "Rahul";

    void display() {
        System.out.println(this.name);
    }

    public static void main(String[] args) {
        Student s1 = new Student();
        s1.display();
    }
}
Output:
Rahul

Here, this.name accesses the name field of the current object.

2. Using this to Access Instance Variables

We can use this to access instance variables explicitly.

class Employee {
    String name;
    int salary;

    Employee(String employeeName, int employeeSalary) {
        this.name = employeeName;
        this.salary = employeeSalary;
    }

    void display() {
        System.out.println(name);
        System.out.println(salary);
    }

    public static void main(String[] args) {
        Employee e1 = new Employee("Amit", 30000);
        e1.display();
    }
}
Output:
Amit
30000

The expression this.name refers to the object's field, while employeeName is the constructor parameter.

3. Resolving Variable Name Conflicts

A common use of this is to distinguish an instance variable from a parameter with the same name.

class Student {
    String name;
    int age;

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

    void display() {
        System.out.println(name + " " + age);
    }

    public static void main(String[] args) {
        Student s1 = new Student("Priya", 21);
        s1.display();
    }
}
Output:
Priya 21
Without this, a parameter with the same name can hide the instance field inside the constructor.
4. Calling an Instance Method Using this

You can use this to call another instance method of the same object.

class Demo {

    void first() {
        System.out.println("First method");
    }

    void second() {
        this.first();
        System.out.println("Second method");
    }

    public static void main(String[] args) {
        Demo d1 = new Demo();
        d1.second();
    }
}
Output:
First method
Second method

The call this.first() invokes the first() method on the current object. In this example, simply writing first() would also work.

5. Calling Another Constructor Using this()

The expression this() can call another constructor in the same class. This is called constructor chaining.

class Student {
    String name;
    int age;

    Student() {
        this("Unknown", 0);
    }

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

    void display() {
        System.out.println(name + " " + age);
    }

    public static void main(String[] args) {
        Student s1 = new Student();
        s1.display();
    }
}
Output:
Unknown 0

The no-argument constructor delegates initialization to the two-parameter constructor.

In standard constructor syntax, this(...) must appear first in the constructor body, subject to the flexible constructor body rules introduced in newer Java versions.
6. Returning the Current Object

An instance method can return this. This is useful when creating methods that can be chained.

class Student {
    String name;

    Student setName(String name) {
        this.name = name;
        return this;
    }

    void display() {
        System.out.println(name);
    }

    public static void main(String[] args) {
        Student s1 = new Student();

        s1.setName("Rahul").display();
    }
}
Output:
Rahul

The setName() method returns the same object, allowing display() to be called immediately afterward.

7. Passing this as an Argument

The current object can be passed as an argument to another method.

class Student {
    String name = "Amit";

    void display() {
        System.out.println(name);
    }

    void sendTo(Printer printer) {
        printer.printStudent(this);
    }
}

class Printer {
    void printStudent(Student student) {
        student.display();
    }
}

class Main {
    public static void main(String[] args) {
        Student s1 = new Student();
        Printer p1 = new Printer();

        s1.sendTo(p1);
    }
}
Output:
Amit

Here, this passes the current Student object to the Printer object's method.

8. Using this in Multiple Objects

Each object has its own current-object reference when its instance methods execute.

class Student {
    String name;

    Student(String name) {
        this.name = name;
    }

    void display() {
        System.out.println(this.name);
    }

    public static void main(String[] args) {
        Student s1 = new Student("Rahul");
        Student s2 = new Student("Priya");

        s1.display();
        s2.display();
    }
}
Output:
Rahul
Priya

When s1.display() executes, this refers to the object referenced by s1. When s2.display() executes, it refers to the object referenced by s2.

9. this Keyword in Static Methods

The this keyword cannot be used directly inside a static method because a static method is associated with the class, not a particular object.

class Demo {

    void display() {
        System.out.println("Instance method");
    }

    static void show() {
        System.out.println("Static method");

        // this.display(); // Compilation error
    }

    public static void main(String[] args) {
        Demo.show();

        Demo d1 = new Demo();
        d1.display();
    }
}
Output:
Static method
Instance method
Do not use this in a static context. Create or receive an object reference if you need to access an instance method from a static method.
10. Difference Between this and super
this super
Refers to the current object. Refers to the superclass portion of the current object.
Can access current-class instance members. Can access accessible superclass members.
this() calls another constructor in the same class. super() calls a superclass constructor.
class Animal {
    String name = "Animal";
}

class Dog extends Animal {
    String name = "Dog";

    void display() {
        System.out.println(this.name);
        System.out.println(super.name);
    }

    public static void main(String[] args) {
        Dog d1 = new Dog();
        d1.display();
    }
}
Output:
Dog
Animal
11. Common Mistakes with this

Mistake 1: Using this in a static method

static void show() {
    // this.name = "Rahul"; // Compilation error
}

Mistake 2: Confusing a parameter with a field

class Student {
    String name;

    Student(String name) {
        this.name = name;
    }
}

Using this.name makes it clear that the field is being assigned the parameter's value.

Mistake 3: Calling this() incorrectly

Student() {
    // System.out.println("Start");
    // this("Rahul"); // Invalid placement in traditional syntax
}

Student(String name) {
    this.name = name;
}

A constructor delegation call must follow the applicable constructor-body rules.

12. Real-Life Example: Employee Details
class Employee {
    String name;
    double salary;

    Employee(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }

    Employee updateSalary(double salary) {
        this.salary = salary;
        return this;
    }

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

    public static void main(String[] args) {
        Employee e1 = new Employee("Amit", 25000);

        e1.updateSalary(30000).display();
    }
}
Output:
Name: Amit
Salary: 30000.0

This example uses this to assign fields and return the current object for method chaining.

13. Practice Assignments
  1. Create a Student class and use this to assign the name and age constructor parameters to instance fields.
  2. Create an Employee class with a method that returns this.
  3. Create two constructors in a Book class and use this() to call one constructor from another.
  4. Create a class with two instance methods and call one method from another using this.
  5. Create a Student and Printer class to pass the current Student object to another method.
  6. Create Animal and Dog classes to demonstrate the difference between this and super.
  7. Create an Employee class with a salary update method that returns the current object.
Practice Tip: Pay attention to which object is executing the method and how this identifies that object.

🧠 Quick Quiz

1. What does this refer to in an instance method?




2. Which expression calls another constructor in the same class?




3. Can this be used directly inside a static method?