All topics
library
beginner

The this & super Keywords

Know every use of this and super: field access, constructor chaining, method calls, and passing the current instance.

this is the current object. super points the compiler at the parent class.

this = pointing at yourself ('I will do this'). super = calling your parent ('Mom, how did you do this?').

Key Concepts

1
this disambiguates a field from a parameter.
    void setName(String name) { this.name = name; }
this
2
Without it, name = name assigns the parameter to itself and the field never changes.
name = name
3
this(...) calls another constructor in the same class, and must be the first statement:
this(...)
4
    Account(String id)                { this(id, 0); }
    Account(String id, double bal)    { this.id = id; this.balance = bal; }
5
That keeps the real work in one constructor instead of duplicating it.
6
this also passes the current object along, and returning it gives a fluent API:
this
7
    Builder name(String n) { this.name = n; return this; }
8
    new Builder().name("Asha").age(30).build();
9
Outer.this reaches the enclosing instance from a non-static inner class, where plain this means the inner object.
Outer.thisthis
10
super(...) calls the parent constructor, also first statement:
super(...)
11
    Dog(String name) { super(name); this.breed = "Lab"; }
12
Omit it and the compiler inserts super(). If the parent has no no-argument constructor, that fails and you must call super(...) yourself.
super()super(...)
13
super.method() extends rather than replaces:
    @Override
    void speak() { super.speak(); print("Woof"); }
super.method()
14
The rules that trip people up.
    this(...) and super(...) cannot both appear -- both must be first
    neither works in a static method
    you cannot read `this` before super(...) has finished
this
15
The last one has a real consequence. If a parent constructor calls an overridable method, the override runs before the child's fields are assigned, so it sees them at their defaults — null and 0. Never call an overridable method from a constructor.
null0
16
What the interviewer is probing.1. "Can this() and super() both appear in one constructor?" Probing: constructor chaining rules. Stalls: "Yes, call the parent then the sibling." Moves up: no — each must be the first statement, so only one can be.
17
2. "A parent constructor calls an overridable method. What goes wrong?" Probing: initialisation order, which is a real bug source. Stalls: "It calls the override, which is fine." Moves up: the override runs before the subclass fields are assigned, so it sees them at their defaults — null and zero. Never call an overridable method from a constructor.
18
3. "Why does a setter write this.name = name?" Probing: shadowing. Stalls: "Convention." Moves up: the parameter shadows the field, so the unqualified name refers to the parameter and name = name assigns it to itself.
19
4. "What does Outer.this mean?" Probing: inner class scoping. Stalls: "The outer class." Moves up: the enclosing instance from a non-static inner class, where plain this refers to the inner object.