100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Java

this Keyword in Java

Master the `this` keyword in Java — resolving field/parameter name conflicts, constructor chaining, and passing the current object.

OOP BasicsBeginner8 min readJul 7, 2026
Analogies

1. Introduction

The this keyword in Java is a reference variable that refers to the current object — the object whose method or constructor is currently executing. It is implicitly available inside every instance method and constructor.

🏏

Cricket analogy: When Virat Kohli is at the crease, "this" is like him referring to himself as "I" during a mid-pitch chat with the umpire — every instance method automatically knows which batsman is currently playing.

this is most commonly used to disambiguate between instance fields and method/constructor parameters that share the same name, to invoke another constructor of the same class, or to pass the current object as an argument to another method.

🏏

Cricket analogy: "this" is used like a captain clarifying "my score" versus "the team's score" when both share a name, calling in another batsman via constructor chaining, or handing the bat's data to the coach as an argument.

2. Syntax

java
class ClassName {
    int value;

    ClassName(int value) {
        this.value = value;   // this.field = parameter
    }

    void show() {
        System.out.println(this.value); // explicit reference to current object's field
    }

    ClassName() {
        this(0);               // constructor chaining
    }
}

3. Explanation

When a constructor or method parameter has the same name as an instance field, simply writing value = value would assign the parameter to itself, leaving the field untouched — a classic bug. Using this.value = value explicitly tells Java that the left-hand side refers to the instance field, resolving the ambiguity.

🏏

Cricket analogy: Writing "score = score" is like a scorer copying the scoreboard's own number onto itself instead of recording Sachin's new runs; this.score = score explicitly assigns the batsman's new tally to the scoreboard field.

this can also be used as this(...) to call another constructor in the same class (constructor chaining), and it can be passed as an argument (this) when an object needs to hand a reference to itself to another method, such as in event listener registration or builder patterns.

🏏

Cricket analogy: A team's shorter T20-squad constructor calls this(fullSquadDetails) to reuse the full-squad setup, and a player passes "this" to the selection committee's tracking system so it can watch that specific player's stats update live.

Exam trap: this cannot be used inside a static context (static methods or static blocks) because static members belong to the class, not to any specific object instance.

4. Example

java
public class Employee {
    String name;
    double salary;

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

    Employee() {
        this("Unknown", 0.0); // constructor chaining
    }

    void raiseSalary(double percent) {
        this.salary = this.salary + (this.salary * percent / 100);
    }

    public static void main(String[] args) {
        Employee e = new Employee("Meera", 50000);
        e.raiseSalary(10);
        System.out.println(e.name + " now earns " + e.salary);

        Employee e2 = new Employee();
        System.out.println(e2.name + " earns " + e2.salary);
    }
}

5. Output

text
Meera now earns 55000.0
Unknown earns 0.0

6. Key Takeaways

  • this refers to the current object instance inside instance methods and constructors.
  • It resolves naming conflicts between instance fields and parameters/local variables.
  • this(...) invokes another constructor of the same class and must be the first statement.
  • this can be passed as an argument to represent the current object.
  • this cannot be used in a static context since it needs an object instance.

Practice what you learned

Was this page helpful?

Topics covered

#Java#JavaProgrammingStudyNotes#Programming#ThisKeywordInJava#Keyword#Syntax#Explanation#Example#StudyNotes#SkillVeris