Ever wonder how your program knows which variable belongs to which object? The answer is <strong>this</strong>!
Why This keyword usage in Java? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you are writing a program with many variables and methods inside a class. You want to clearly tell which variable belongs to the current object, but you keep mixing up local variables and object variables.
Without a clear way to refer to the current object's variables, your code becomes confusing and error-prone. You might accidentally change the wrong variable or write unclear code that others find hard to understand.
The this keyword lets you clearly point to the current object's variables and methods. It removes confusion by showing exactly what belongs to the object, making your code cleaner and safer.
public class Car { String model; public void setModel(String model) { model = model; // Confusing, does not set object variable } }
public class Car {
String model;
public void setModel(String model) {
this.model = model; // Clear: sets object variable
}
}Using this makes your code clear and helps avoid mistakes when working with object data.
When building a game, you might have many characters with similar properties. Using this helps each character keep track of its own health and position without confusion.
this points to the current object.
It helps distinguish object variables from local ones.
Using this makes code clearer and less error-prone.
Practice
this keyword refer to in a Java class?Solution
Step 1: Understand the role of
Thethisthiskeyword 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 BQuick Check:
this= current object instance [OK]
this means "this object" [OK]- Confusing
thiswith static context - Thinking
thisrefers to superclass - Assuming
thisis a local variable
this to call another constructor in the same class?Solution
Step 1: Identify constructor call syntax
In Java, calling another constructor in the same class usesthis();as the first statement.Step 2: Eliminate incorrect options
super();calls superclass constructor,this.call();is invalid syntax, andcall(this);is not a constructor call.Final Answer:
this(); -> Option DQuick Check:
Constructor chaining usesthis();[OK]
this(); to call another constructor [OK]- Using
super();instead ofthis(); - Trying to call constructor like a method
- Placing
this();not as first statement
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();
}
}Solution
Step 1: Identify local and instance variables
InsideprintX(), localxis 20, instancexis 10.Step 2: Understand
this.xusageSystem.out.println(x);prints local 20,System.out.println(this.x);prints instance 10.Final Answer:
20 10 -> Option CQuick Check:
Local x = 20, this.x = 10 [OK]
this for instance [OK]- Confusing local and instance variables
- Ignoring
thiskeyword - Expecting both prints to be same
class Sample {
int value;
Sample(int value) {
value = value;
}
}Solution
Step 1: Analyze assignment in constructor
The statementvalue = value;assigns the parameter to itself, not to the instance variable.Step 2: Correct usage with
To assign parameter to instance variable, usethisthis.value = value;.Final Answer:
The constructor does not assign parameter to instance variable -> Option AQuick Check:
Missingthis.causes no instance update [OK]
this.variable = variable; to assign correctly [OK]- Assuming parameter assigns instance variable automatically
- Thinking same names cause syntax error
- Ignoring need for
thiskeyword
class Point {
int x, y;
Point() {
this(0, 0);
}
Point(int x, int y) {
this.x = x;
this.y = y;
}
void move(int x, int y) {
x = x;
this.y = y;
}
String display() {
return "(" + this.x + ", " + this.y + ")";
}
public static void main(String[] args) {
Point p = new Point();
p.move(5, 10);
System.out.println(p.display());
}
}What will be the output when running the
main method?Solution
Step 1: Understand constructor chaining
The no-arg constructor callsthis(0, 0);settingx=0andy=0.Step 2: Analyze
Insidemovemethodmove,x = x;assigns parameter to itself, so instancexremains 0.this.y = y;updates instanceyto 10.Step 3: Check
Returns string with instance variables:displayoutput(0, 10).Final Answer:
(0, 10) -> Option AQuick Check:
this.xunchanged,this.yupdated [OK]
this.var = var; inside methods [OK]- Assuming
x = x;updates instance variable - Ignoring constructor chaining effect
- Expecting both coordinates to update
