this Keyword
The this keyword in Java is a reference variable that refers to the current object of a class. It is primarily used to resolve ambiguity, improve code clarity, and support constructor chaining. This is a high-frequency interview topic and essential for clean object-oriented design.
What Is this?
- Refers to the current object
- Used inside instance methods and constructors
- Cannot be used in a static context
- Enhances readability and correctness
Basic Syntax
this.variableName
Why this Keyword Is Needed
Problem Without this
class Student {
int age;
Student(int age) {
age = age; // ambiguous
}
}
❌ Instance variable is not updated.
Solution Using this
class Student {
int age;
Student(int age) {
this.age = age;
}
}
✔ Correctly assigns value to instance variable.
Uses of this Keyword (Very Important)
1. Refers to Instance Variables
Resolves name conflict between parameters and instance variables.
this.age = age;
2. Invokes Current Class Method
void display() {
System.out.println("Display");
}
void show() {
this.display();
}
✔ this is optional here, but improves clarity.
3. Constructor Chaining Using this()
Calls another constructor of the same class.
class Test {
Test() {
this(10);
System.out.println("No-arg");
}
Test(int a) {
System.out.println("Parameterized: " + a);
}
}
Rules:
- this() must be the first statement
- Only one this() allowed per constructor
4. Passing Current Object as Argument
void show(Student s) {
System.out.println(s.age);
}
void call() {
show(this);
}
✔ Useful for callback or chaining scenarios.
5. Returning Current Object
Student getObject() {
return this;
}
✔ Supports method chaining.
Where this Cannot Be Used
❌ Static Context
static void test() {
// this.x = 10; // Compile-time error
}
Reason: Static methods belong to the class, not objects.
this vs super
| Feature | this | super |
|---|---|---|
| Refers to | Current class object | Parent class object |
| Accesses | Current class members | Parent class members |
| Constructor call | this() | super() |
| Static context | ❌ Not allowed | ❌ Not allowed |
Common Beginner Mistakes
- Forgetting this in constructors
- Using this in static methods
- Placing this() not as first statement
- Confusing this with super
Interview-Ready Answers
Short Answer
The this keyword refers to the current object of a class.
Detailed Answer
In Java, the this keyword is a reference to the current object. It is used to resolve ambiguity between instance variables and parameters, call current class constructors or methods, pass the current object as an argument, and support method chaining.
this Keyword Examples (Interview-Focused)
1. Referring to Instance Variable (Shadowing Fix)
class Demo {
int x;
Demo(int x) {
this.x = x;
}
public static void main(String[] args) {
Demo d = new Demo(10);
System.out.println(d.x);
}
}
Explanation: this.x refers to instance variable.
Output: 10
2. Shadowing Bug Without this
class Demo {
int x;
Demo(int x) {
x = x; // wrong
}
public static void main(String[] args) {
Demo d = new Demo(20);
System.out.println(d.x);
}
}
Explanation: Parameter shadows instance variable.
Output: 0
3. Using this to Call Another Constructor (this())
class Demo {
int x;
Demo() {
this(5);
}
Demo(int x) {
this.x = x;
}
public static void main(String[] args) {
Demo d = new Demo();
System.out.println(d.x);
}
}
Explanation: Constructor chaining.
Output: 5
4. this() Must Be First Statement
class Demo {
Demo() {
// System.out.println("Hi");
// this(10); // Compile-time error
}
Demo(int x) {}
}
Explanation: this() must be first line in constructor.
5. Using this to Call Instance Method
class Demo {
void show() {
System.out.println("Show");
}
void display() {
this.show();
}
public static void main(String[] args) {
new Demo().display();
}
}
Explanation: Calls current object method.
Output: Show
6. Calling Instance Method Without this
class Demo {
void show() {
System.out.println("Show");
}
void display() {
show(); // implicitly this.show()
}
public static void main(String[] args) {
new Demo().display();
}
}
Explanation: this is implicit.
Output: Show
7. Passing this as Method Argument
class Demo {
void print(Demo d) {
System.out.println(d);
}
void call() {
print(this);
}
public static void main(String[] args) {
new Demo().call();
}
}
Explanation: Passes current object reference.
Output: object reference string
8. Passing this to Another Class
class Test {
Test(Demo d) {
System.out.println("Received object");
}
}
class Demo {
void send() {
new Test(this);
}
public static void main(String[] args) {
new Demo().send();
}
}
Explanation: Common in callbacks.
Output: Received object
9. Returning this from Method (Method Chaining)
class Demo {
int x;
Demo set(int x) {
this.x = x;
return this;
}
public static void main(String[] args) {
Demo d = new Demo().set(10).set(20);
System.out.println(d.x);
}
}
Explanation: Enables chaining.
Output: 20
10. this with Multiple Instance Variables
class Demo {
int a, b;
Demo(int a, int b) {
this.a = a;
this.b = b;
}
public static void main(String[] args) {
Demo d = new Demo(1, 2);
System.out.println(d.a + " " + d.b);
}
}
Explanation: Distinguishes instance vs parameters.
Output: 1 2
11. Using this Inside Setter Method
class User {
String name;
void setName(String name) {
this.name = name;
}
public static void main(String[] args) {
User u = new User();
u.setName("Admin");
System.out.println(u.name);
}
}
Explanation: Common real-world usage.
Output: Admin
12. this in Instance Block
class Demo {
int x;
{
this.x = 10;
}
public static void main(String[] args) {
Demo d = new Demo();
System.out.println(d.x);
}
}
Explanation: Instance block has access to this.
Output: 10
13. this Cannot Be Used in Static Context
class Demo {
static void test() {
// System.out.println(this); // Compile-time error
}
}
Explanation: this belongs to object, not class.
14. Differentiating Two Objects Using this
class Demo {
int x;
void set(int x) {
this.x = x;
}
public static void main(String[] args) {
Demo d1 = new Demo();
Demo d2 = new Demo();
d1.set(5);
d2.set(10);
System.out.println(d1.x + " " + d2.x);
}
}
Explanation: this refers to calling object.
Output: 5 10
15. this in Copy Constructor
class User {
String name;
User(String name) {
this.name = name;
}
User(User u) {
this.name = u.name;
}
public static void main(String[] args) {
User u1 = new User("Admin");
User u2 = new User(u1);
System.out.println(u2.name);
}
}
Explanation: Copies state from another object.
Output: Admin
16. this with Method Overloading
class Demo {
void show() {
System.out.println("No args");
}
void show(int x) {
this.show();
System.out.println(x);
}
public static void main(String[] args) {
new Demo().show(10);
}
}
Explanation: Calls overloaded method.
Output:
No args
10
17. this vs Local Object Reference
class Demo {
void test() {
Demo d = this;
System.out.println(d == this);
}
public static void main(String[] args) {
new Demo().test();
}
}
Explanation: Both point to same object.
Output: true
18. this with Fluent API Style
class Builder {
int x;
Builder add(int v) {
this.x += v;
return this;
}
public static void main(String[] args) {
Builder b = new Builder().add(5).add(10);
System.out.println(b.x);
}
}
Explanation: Fluent design pattern.
Output: 15
19. Common Interview Trap
class Demo {
int x;
Demo(int x) {
this.x = x;
x = 50;
}
public static void main(String[] args) {
Demo d = new Demo(10);
System.out.println(d.x);
}
}
Explanation: Instance variable already set.
Output: 10
20. Interview Summary – this Keyword
class Demo {
int x;
Demo set(int x) {
this.x = x;
return this;
}
public static void main(String[] args) {
System.out.println(new Demo().set(25).x);
}
}
Explanation: Refers to current object, resolves shadowing, enables chaining.
Output: 25
Key Takeaway
The this keyword provides clarity, correctness, and control in object-oriented programming. Proper use of this leads to cleaner and more maintainable Java code.