Java Reference ยท Keywords and Statements
The this and super keywords
Reference for Java this and super: refer to the current object, chain constructors, call overridden parent methods, and avoid common constructor mistakes.
this is a reference to the object whose method or constructor is running. super refers to the parent-class part of that same object. Neither exists in a static context, because there is no current object.
Both have a second form used in constructors: this(...) calls another constructor of the same class, and super(...) calls a constructor of the parent class. Such a call must be the first statement of the constructor, except that Java 25 allows statements that do not touch this before it (flexible constructor bodies, JEP 513).
class Child extends Parent { Child(String n) { super(n); } // parent constructor Child() { this("default"); } // sibling constructor void show() { System.out.println(this.name + super.toString()); } }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
| Form | Meaning |
|---|---|
this |
Reference to the current object. Usable in instance methods, constructors and initializers. |
this.field |
The field, even when a local or parameter has the same name. |
this.method() |
Same as method(), written explicitly. |
this(args) |
Invoke another constructor of the same class. |
super.method() |
Call the parent's version of an overridden method. |
super.field |
Parent field (rarely needed). |
super(args) |
Invoke a parent constructor. |
Interface.super.method() |
Call a specific interface default method. |
Outer.this |
The enclosing instance of an inner class. |
Usage notes
- Implicit
super(): if you do not callthis(...)orsuper(...), the compiler insertssuper(). If the parent has no no-argument constructor, compilation fails. - One chained call only: a constructor cannot contain both
this(...)andsuper(...); the chain eventually must reach asuper(...). - No cycles: constructors that call each other with
this(...)in a loop are rejected. - Leaking
this: passingthisto another thread or registering it as a listener from the constructor can expose a half-built object. Calling an overridable method from a constructor is risky for the same reason. - Static context:
thisandsuperare errors in static methods and static initializers. - Method chaining: returning
thismakes fluent builders; return a new object for immutable types. - Lambdas versus anonymous classes: inside a lambda
thismeans the enclosing instance; inside an anonymous class it is the anonymous object. supercannot skip levels:super.super.x()is not valid.- Records: compact constructors cannot use
this.field = ...; fields are assigned automatically at the end.