Skip to content
  • this & super · Java

this & super

LearnViaNotes @learnvianotes· 2 min read

this refers to the current object; super refers to the superclass part of the current object.

Uses of this

UseExampleWhy
Disambiguate a field from a parameterthis.name = name;Parameter shadows the field
Call another constructorthis(name, 0);Constructor chaining — first statement only
Pass the current objectregistry.add(this);Callbacks, listeners
Return the current objectreturn this;Fluent / builder APIs
Java
public class Main {
    public static void main(String[] args) {
        Counter c = new Counter().add(5).add(10).add(-3);   // chaining via return this
        System.out.println(c.total);
    }
}

class Counter {
    int total;
    Counter add(int total) {
        this.total += total;   // this.total = field, total = parameter
        return this;
    }
}
Output
12

Uses of super

UseExample
Call a superclass constructorsuper(name); — first statement of the constructor
Call the overridden superclass methodsuper.describe()
Access a hidden superclass fieldsuper.name (when the subclass declares a field with the same name)
Java
public class Main {
    public static void main(String[] args) {
        Manager m = new Manager("Asha", 3);
        System.out.println(m.describe());
        m.showNames();
    }
}

class Employee {
    protected String name;
    String kind = "employee";
    Employee(String name) { this.name = name; }
    String describe() { return name + " (" + kind + ")"; }
}

class Manager extends Employee {
    private final int reports;
    String kind = "manager";                   // hides Employee.kind

    Manager(String name, int reports) {
        super(name);                           // must come first
        this.reports = reports;
    }

    @Override String describe() {
        return super.describe() + " manages " + reports;   // reuse parent logic
    }

    void showNames() {
        System.out.println(this.kind + " / " + super.kind);
    }
}
Output
Asha (employee) manages 3
manager / employee

Rules

  • this and super cannot be used in a static context — there is no current object.

  • A constructor can start with this(...) or super(...), never both.

  • super.super.method() is not allowed — you can only reach one level up.

  • If the superclass has no no-arg constructor, every subclass constructor must call super(args) explicitly.

Java
class Animal {
    Animal(String name) { }
}

class Dog extends Animal {
    Dog() { }   // implicit super() — but Animal has no no-arg constructor
}
javac
Main.java:6: error: constructor Animal in class Animal cannot be applied to given types;
    Dog() { }   // implicit super() — but Animal has no no-arg constructor
          ^
  required: String
  found:    no arguments
  reason: actual and formal argument lists differ in length

Why can't this be used inside a static method?

What happens if you don't call super(...) in a constructor?

Free for students and teachers

Write notes like this one

Cards for every idea, quizzes and flashcards, read aloud — on the web and your phone.

Something wrong with this note? Open it to report