this & super
this and super are two reference keywords in Java. this refers to the
current object — the instance whose method or constructor is currently running. super
refers to the parent class of the current object. Both show up constantly in constructors and
inheritance-related code.
this — Referring to the Current Object
The most common use of this is disambiguating a field from a parameter that share the same name — exactly the situation we saw on the Constructors page:
public class Book {
String title;
Book(String title) {
this.title = title; // this.title is the field, title is the parameter
}
}
Without this, writing title = title; would just assign the parameter to itself and leave the field untouched. this.title makes clear you mean the field that belongs to the object.
Calling Another Constructor with this(...)
this(...), used as the first line of a constructor, calls another constructor in the same class. It's a handy way to avoid repeating setup logic across overloaded constructors:
public class Book {
String title;
int pages;
Book(String title) {
this(title, 0); // delegates to the constructor below
}
Book(String title, int pages) {
this.title = title;
this.pages = pages;
}
}
this(...) must be the very first statement in a constructor, and a constructor can only call one other constructor this way.super — Referring to the Parent Class
Recall the Inheritance page, where a subclass extends a parent class. super lets the subclass reach back into that parent — to call an overridden method's original version, or to run the parent's constructor.
public class Animal {
String name;
Animal(String name) {
this.name = name;
}
void makeSound() {
System.out.println("Some generic animal sound");
}
}
public class Dog extends Animal {
Dog(String name) {
super(name); // calls Animal's constructor
}
void makeSound() {
super.makeSound(); // runs Animal's version first
System.out.println(name + " barks");
}
}
super(name) forwards the constructor argument up to Animal's constructor. super.makeSound() calls the parent's original overridden method, before Dog adds its own behavior on top.
this(...), a call to super(...) must be the first statement in a constructor. If you don't write one explicitly, Java inserts a no-argument super() call for you.this vs super — Side by Side
this | super | |
|---|---|---|
| Refers to | The current object | The parent class of the current object |
| Field/method access | this.field, this.method() | super.field, super.method() |
| Constructor call | this(...) — another constructor in the same class | super(...) — the parent class's constructor |
| Typical purpose | Disambiguate fields from parameters; avoid duplicate constructor logic | Reuse or extend parent behavior; initialize inherited state |
| Placement rule | If used to call a constructor, must be the first statement | If used to call a constructor, must be the first statement |
Quick recap:
- this — "the object I'm currently working with."
- super — "the parent class this object was built from."
- Both can call a constructor, but only as the first line, and never both in the same constructor.