Skip to main content

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.

L1

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

  • this is 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 this or take its address.
  • The compiler passes this implicitly 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 = field when a parameter shadows a field.
  • this() to chain constructors (first statement only).
  • this as an argument: list.add(this), other.attach(this).
  • this.method() is optional but can clarify instance vs static.
SyntaxMeaning
this.xField / method of the current instance
this(...)Call another constructor in the same class
return thisFluent API / current object
this in static contextCompile error — no instance

this vs super (preview)

  • this is this instance (possibly as a subclass). super reaches the superclass implementation or constructor.
  • You cannot assign this = .... It is final in spirit.
L2

Advanced — this Keyword

Inner classes and escaping this

  • In an inner class, Outer.this refers to the enclosing instance; plain this is the inner object.
  • Publishing this from a constructor (listeners.add(this) before the object is fully built) is a concurrency / visibility hazard.
  • Fluent setters return this; enable new Builder().name("a").age(2). Records and builders often replace long setter chains.
L3

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.

L4

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);
    }
}
L5

Problem Solving Tricks

  • If you never shadow fields, you can omit this. — but constructors almost always shadow.
  • Fluent return this is great for builders; do not use it to hide missing immutability.
  • Outer.this is the fix when an inner class parameter hides an outer field.
  • A static nested class has no hidden this of the outer class — that is why it is preferred when you do not need the outer instance.
L6

Practice Programs List

  1. Write a class where removing this. silently assigns a parameter to itself (no field update) — then fix it.
  2. Chain two constructors with this().
  3. Implement three fluent setters that return this.
  4. Try to print this from main without an instance and read the error.
  5. From an inner class, print both this and Outer.this.