The this keyword helps you refer to the current object inside a class. It makes your code clear and avoids confusion when names overlap.
When you want to refer to the current object's fields inside a method.
When constructor parameters have the same names as class fields.
When you want to call one constructor from another in the same class.
When you want to pass the current object as a parameter to another method.
Syntax
Java
this.fieldName
this.methodName()
this(parameters) // to call another constructor
this always refers to the current object instance.
Use this to avoid confusion between local variables and object fields.
Examples
Here, this.color means the field of the object, and color is the constructor parameter.
Java
class Car {
String color;
Car(String color) {
this.color = color; // 'this.color' is the field, 'color' is the parameter
}
}
this.name refers to the field, so we can set it using the parameter name.
Java
class Person {
String name;
void setName(String name) {
this.name = name; // Assign parameter to the object's field
}
}
this(10, 20) calls another constructor in the same class.
Java
class Box {
int width, height;
Box() {
this(10, 20); // Calls the other constructor
}
Box(int w, int h) {
width = w;
height = h;
}
}
Sample Program
This program shows how this assigns the constructor parameter to the object's field and then prints it.
Java
publicclass ThisExample {
int x;
ThisExample(int x) {
this.x = x; // Assign parameter x to field x
}
void printX() {
System.out.println("Value of x: " + this.x);
}
publicstaticvoid main(String[] args) {
ThisExample obj = new ThisExample(5);
obj.printX();
}
}
OutputSuccess
Important Notes
You cannot use this in static methods because static methods belong to the class, not an object.
Using this improves code readability and helps avoid mistakes when names overlap.
Summary
this refers to the current object instance.
Use this to access fields or methods of the current object.
this can call another constructor in the same class.
Practice
(1/5)
1. What does the this keyword refer to in a Java class?
easy
A. A static method
B. The current object instance
C. A superclass object
D. A local variable
Solution
Step 1: Understand the role of this
The this keyword always points to the current object instance inside a class.
Step 2: Differentiate from other options
It does not refer to static methods, superclass objects, or local variables.
Final Answer:
The current object instance -> Option B
Quick Check:
this = current object instance [OK]
Hint: Remember: this means "this object" [OK]
Common Mistakes:
Confusing this with static context
Thinking this refers to superclass
Assuming this is a local variable
2. Which of the following is the correct way to use this to call another constructor in the same class?
easy
A. call(this);
B. super();
C. this.call();
D. this();
Solution
Step 1: Identify constructor call syntax
In Java, calling another constructor in the same class uses this(); as the first statement.
Step 2: Eliminate incorrect options
super(); calls superclass constructor, this.call(); is invalid syntax, and call(this); is not a constructor call.
Final Answer:
this(); -> Option D
Quick Check:
Constructor chaining uses this(); [OK]
Hint: Use this(); to call another constructor [OK]
Common Mistakes:
Using super(); instead of this();
Trying to call constructor like a method
Placing this(); not as first statement
3. What will be the output of the following code?
class Test {
int x = 10;
void printX() {
int x = 20;
System.out.println(x);
System.out.println(this.x);
}
public static void main(String[] args) {
new Test().printX();
}
}
medium
A. 10\n20
B. 20\n20
C. 20\n10
D. 10\n10
Solution
Step 1: Identify local and instance variables
Inside printX(), local x is 20, instance x is 10.
Step 2: Understand this.x usage
System.out.println(x); prints local 20, System.out.println(this.x); prints instance 10.
Final Answer:
20
10 -> Option C
Quick Check:
Local x = 20, this.x = 10 [OK]
Hint: Local variable hides instance; use this for instance [OK]
Common Mistakes:
Confusing local and instance variables
Ignoring this keyword
Expecting both prints to be same
4. Identify the error in the following code snippet:
class Sample {
int value;
Sample(int value) {
value = value;
}
}
medium
A. The constructor does not assign parameter to instance variable
B. Syntax error: missing this keyword
C. Cannot have parameter and instance variable with same name
D. No error, code works fine
Solution
Step 1: Analyze assignment in constructor
The statement value = value; assigns the parameter to itself, not to the instance variable.
Step 2: Correct usage with this
To assign parameter to instance variable, use this.value = value;.
Final Answer:
The constructor does not assign parameter to instance variable -> Option A
Quick Check:
Missing this. causes no instance update [OK]
Hint: Use this.variable = variable; to assign correctly [OK]