this vs super in Java: Keywords and Constructor Calls

Learn this and super in Java, this() vs super() constructor calls, Interface.super, Java 25 flexible constructor bodies and static context rules.

this and super resolving members of the same Manager object

In Java, this refers to the current object, and super gives access to the members of the parent class that the current class has hidden or overridden. In their call forms, this() runs another constructor of the same class and super() runs a constructor of the parent class.

We use this to tell fields apart from parameters with the same name, to pass the current object to other methods, and to chain constructors. We use super to extend an overridden method instead of replacing it, and to initialize the parent part of a new object.

The following example uses both keywords in a small class hierarchy.

class Employee {
    protected final String name;

    Employee(String name) {
        this.name = name;                // this.name is the field
    }

    String describe() {
        return name;
    }
}

class Manager extends Employee {
    private final int teamSize;

    Manager(String name, int teamSize) {
        super(name);                     // runs Employee(String)
        this.teamSize = teamSize;
    }

    @Override
    String describe() {
        return super.describe() + " leads " + teamSize;   // extends the parent version
    }
}
Employee lead = new Manager("Ann", 4);
String text = lead.describe();          // "Ann leads 4"

Notice that super.describe() calls the parent method, even though the object is a Manager. We cover each use of this and super, constructor chaining, the flexible constructor bodies of Java 25, and the places where neither keyword is allowed.

1. Uses of the this Keyword

The this keyword is a reference to the object whose method or constructor is running. It has four common uses.

this and super resolving members of the same Manager object
Both refer to one object; super only starts the member lookup in the parent class
  • Accessing a field that a parameter or local variable with the same name hides, as in this.name = name.
  • Passing the current object to another method or returning it, as fluent builders do with return this;.
  • Calling another constructor of the same class with this(…).
  • Accessing the outer object from an inner class with Outer.this.
class Order {
    private final List<String> items = new ArrayList<>();
    private String note = "";

    Order add(String item) {
        items.add(item);
        return this;                     // allows chained calls
    }

    Order note(String note) {
        this.note = note;                // field vs parameter
        return this;
    }

    String summary() {
        return items + " " + note;
    }
}
String summary = new Order().add("tea").add("cake").note("table 5").summary();   // "[tea, cake] table 5"

Calling a method as this.describe() is the same as calling describe(). Both calls use dynamic dispatch, so the subclass version runs if one exists, and the prefix only makes the code more explicit.

2. Uses of the super Keyword

The super keyword is not a reference to a separate parent object, because a Manager is one object that includes the fields of Employee. It tells the compiler to look up a member starting in the parent class instead of the current class, and a statement such as Object o = super; does not compile.

  • super.method() calls the parent version of an overridden method.
  • super.field reads a parent field that a field of the same name in the subclass hides.
  • super(…) calls a parent constructor.
  • Interface.super.method() calls a default method of a specific interface.
interface Greeter {
    default String greet() {
        return "Hello";
    }
}

class PoliteGreeter implements Greeter {
    @Override
    public String greet() {
        return Greeter.super.greet() + ", welcome";
    }
}
String greeting = new PoliteGreeter().greet();   // "Hello, welcome"

The interface form is required when a class implements two interfaces with the same default method, because the compiler forces the class to override it and choose one, or combine both.

3. this() vs super() in Constructors

Every constructor, except the one in Object, runs another constructor, either one of its own class with this(…) or one of the parent class with super(…). When we write neither, the compiler inserts super() without arguments, which fails if the parent has no such constructor.

class Room {
    final String name;
    final int capacity;

    Room(String name) {
        this(name, 10);                  // delegates to the full constructor
    }

    Room(String name, int capacity) {
        this.name = name;
        this.capacity = capacity;
    }
}
Room small = new Room("Focus");
int seats = small.capacity;             // 10

A constructor can call this(…) or super(…), but not both, because the delegated constructor already calls super() itself. Chaining with this(…) keeps the validation and field assignments in one constructor.

4. Flexible Constructor Bodies in Java 25

Until Java 24, this(…) or super(…) had to be the first statement of a constructor. Java 25 finalized JEP 513, which allows statements before the call, as long as they do not read the object under construction, although assigning its fields is allowed.

The statements before the call can validate arguments, compute values and assign the fields of the current class. Reading fields or calling instance methods is still forbidden there, because the parent part of the object does not exist yet.

class Base {
    final String created;

    Base() {
        created = "base sees " + label();    // calls the overridden method
    }

    String label() {
        return "?";
    }
}

class Labeled extends Base {
    private final String label;

    Labeled(String rawLabel) {
        if (rawLabel == null || rawLabel.isBlank()) {
            throw new IllegalArgumentException("label required");
        }
        label = rawLabel.trim();         // allowed before super() in Java 25
        super();
    }

    @Override
    String label() {
        return label;
    }
}
String created = new Labeled(" sale ").created;   // "base sees sale"
Labeled bad = new Labeled(" ");                     // IllegalArgumentException

Before Java 25, the parent constructor would have seen null in label, because fields were assigned only after super() returned. Assigning the field first fixes this classic bug of calling overridable methods from a constructor.

5. this and super in Static Context

Neither keyword works in a static method or a static initializer, because static code runs without any object. Using them there is a compile error, such as non-static variable this cannot be referenced from a static context.

The static main() method therefore cannot call instance methods without an object. It creates an object first and calls the methods on that object, and the static keyword tutorial explains the difference between class and instance members.

6. this vs super Compared

thissuper
Refers toThe current objectThe current object, with lookup starting in the parent class
Usable as a valueYes, can be passed or returnedNo
Member accessthis.field, this.method()super.field, super.method()
Constructor callthis(…), same classsuper(…), parent class
Implicit callNeversuper() when no other call is written
Static contextNot allowedNot allowed

7. this and super FAQs

Interviews often include these questions about this and super.

7.1. Can we use this() and super() in the same constructor?

No. A constructor makes only one explicit constructor call. If it calls this(…), the other constructor calls super(…).

7.2. Can we call super.super.method()?

No. Java allows access only to the direct parent through super, which keeps each class in control of how its parent’s behavior is used.

7.3. What is the difference between super and super()?

The plain keyword accesses parent members, such as super.toString(), from any instance method. The call form super() runs a parent constructor and can appear only in a constructor.

7.4. Is super() still required to be the first statement?

Not since Java 25. Statements that do not use the current object can come before it, as JEP 513 allows. In Java 24 and older, it must be the first statement.

8. Conclusion

The this keyword refers to the current object and resolves name clashes, returns the object for chaining and delegates between constructors. The super keyword starts member lookup in the parent class and calls parent constructors, and it is not a separate object.

Every constructor runs this(…) or super(…), written or implicit. Since Java 25, validation and field assignments can come before that call, and neither keyword is available in static code.

9. References

Happy Learning !!

Source Code on Github

Leave a Comment

Comments are closed.

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.