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
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
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
Meera now earns 55000.0
Unknown earns 0.06. Key Takeaways
thisrefers 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.thiscan be passed as an argument to represent the current object.thiscannot be used in a static context since it needs an object instance.
Practice what you learned
1. What does the `this` keyword refer to?
2. Why is `this.value = value;` needed when a parameter has the same name as a field?
3. Where can `this(...)` appear in a constructor?
4. Can `this` be used inside a static method?
5. In the Employee example, what does calling new Employee() print?
Was this page helpful?
You May Also Like
Constructors in Java
Understand Java constructors — default, parameterized, overloaded, and chained — with syntax rules and worked examples.
Classes and Objects in Java
Learn how classes act as blueprints and objects as instances in Java, with syntax, examples, and exam-focused explanations.
Inheritance in Java
Understand Java inheritance using the extends keyword, the super keyword, single inheritance rules, and the Object root class.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics