Skip to content
Tutorials Center
My learning

Topics

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).

Java
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

Java this: fields, chaining and returning the object

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.

Java super: constructors and overridden methods

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 call this(...) or super(...), the compiler inserts super(). If the parent has no no-argument constructor, compilation fails.
  • One chained call only: a constructor cannot contain both this(...) and super(...); the chain eventually must reach a super(...).
  • No cycles: constructors that call each other with this(...) in a loop are rejected.
  • Leaking this: passing this to 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: this and super are errors in static methods and static initializers.
  • Method chaining: returning this makes fluent builders; return a new object for immutable types.
  • Lambdas versus anonymous classes: inside a lambda this means the enclosing instance; inside an anonymous class it is the anonymous object.
  • super cannot skip levels: super.super.x() is not valid.
  • Records: compact constructors cannot use this.field = ...; fields are assigned automatically at the end.