Java OOP
this Keyword
Complete lesson for this Keyword using LEARNCODEGURU’s 6-level learning model — from basics to advanced practice, with Java OOP and Collections where relevant.
Basics — this Keyword
Section: Java OOP. this is the current instance reference — not a C++ this pointer. Fields, chaining, and passing this.
What this means
thisis a reference to the **current object** — the instance whose method or constructor is running.- It is **not** a pointer (Java has no pointer arithmetic). You cannot increment
thisor take its address. - The compiler passes
thisimplicitly to every instance method.
public class ThisDemo {
String name;
ThisDemo(String name) {
this.name = name; // parameter hides the field
}
ThisDemo self() {
return this;
}
void print(ThisDemo other) {
System.out.println(this.name + " vs " + other.name);
}
public static void main(String[] args) {
ThisDemo a = new ThisDemo("left");
ThisDemo b = new ThisDemo("right");
a.print(b);
System.out.println(a.self().name);
}
} Four everyday uses
this.field = fieldwhen a parameter shadows a field.this()to chain constructors (first statement only).thisas an argument:list.add(this),other.attach(this).this.method()is optional but can clarify instance vs static.
| Syntax | Meaning |
|---|---|
this.x | Field / method of the current instance |
this(...) | Call another constructor in the same class |
return this | Fluent API / current object |
this in static context | Compile error — no instance |
this vs super (preview)
thisis this instance (possibly as a subclass).superreaches the superclass implementation or constructor.- You cannot assign
this = .... It is final in spirit.
Advanced — this Keyword
Inner classes and escaping this
- In an inner class,
Outer.thisrefers to the enclosing instance; plainthisis the inner object. - Publishing
thisfrom a constructor (listeners.add(this)before the object is fully built) is a concurrency / visibility hazard. - Fluent setters
return this;enablenew Builder().name("a").age(2). Records and builders often replace long setter chains.
Tricky Interview Points
Q1) What is this in Java?
Answer A reference to the current instance. It is not a pointer and cannot be used in a static method.
Q2) Why do we write this.name = name?
Answer The parameter name hides the field. this.name is the field; name is the parameter.
Q3) What is this()?
Answer A call to another constructor of the same class. It must be the first statement.
Q4) Can you use this in a static method?
Answer No. static methods have no current instance. The compiler error is “non-static variable this cannot be referenced from a static context”.
Q5) this vs super?
Answer this is the current object / same-class constructor. super is the superclass constructor or hidden/overridden members.
Q6) Is this a pointer like in C++?
Answer No. Interviews should say “current instance reference”. No arithmetic, no ->, no null this inside an instance method.
Simple Programs
Fluent this and constructor chaining
public class Pen {
String color;
int ink;
Pen() {
this("blue", 100);
}
Pen(String color, int ink) {
this.color = color;
this.ink = ink;
}
Pen refill(int extra) {
this.ink += extra;
return this;
}
public static void main(String[] args) {
Pen p = new Pen().refill(20).refill(5);
System.out.println(p.color + " " + p.ink);
}
} Problem Solving Tricks
- If you never shadow fields, you can omit
this.— but constructors almost always shadow. - Fluent
return thisis great for builders; do not use it to hide missing immutability. Outer.thisis the fix when an inner class parameter hides an outer field.- A static nested class has no hidden
thisof the outer class — that is why it is preferred when you do not need the outer instance.
Practice Programs List
- Write a class where removing
this.silently assigns a parameter to itself (no field update) — then fix it. - Chain two constructors with
this(). - Implement three fluent setters that
return this. - Try to print
thisfrommainwithout an instance and read the error. - From an inner class, print both
thisandOuter.this.