this and super
this is the current object. super is the inherited part of that object. You use them to pick a field, call a method, or chain a constructor when names collide or the parent must run first.
Detailed Explanation
Inside an instance method, the object you called the method on is this. The compiler already uses it: amount in a method body means this.amount when no local variable hides the field. You write this when a parameter has the same name, when you pass the current object to someone else, or when you chain constructors with this(...).
super names the superclass view. super.draw() calls the parent's draw even if this class overrides it. super(name) runs a parent constructor. There is no super object sitting beside this. There is one object, laid out with parent fields first, and super is how you address that inherited slice.
These keywords exist because names collide on purpose. Constructors take parameters named after fields. Overrides replace methods you still need to reuse. Without this and super, you would invent unique names everywhere or lose access to the parent implementation.
Constructor forms are stricter than method forms. this(...) and super(...) may appear only as the first statement of a constructor, and only one of them. After that, the object is far enough along that ordinary this.field and super.method() calls are legal in the rest of the body. You cannot write this(...) inside a method. You cannot store super in a variable.
this also appears in a few other places. An inner class instance holds a reference to its enclosing instance, written Outer.this. A fluent setter can return this so callers chain calls. A listener can pass this as the callback target. All of those are the same idea: the current object, explicitly.
You reach for this when a name is ambiguous or when the object itself is the value you need to hand over. You reach for super when an override must extend parent behaviour instead of replacing it, or when a subclass constructor must feed arguments upward. If you find yourself calling super for most methods, the subclass may be the wrong split; composition often fits better.
The cost of overusing them is noise. this.x = x is useful. Prefixing every field with this. when nothing shadows it is style, not safety. Calling super from many overrides can freeze the parent API into the child and make later parent changes break you.
Code Examples
public class Account {
private int balance;
public Account(int balance) {
this.balance = balance; // parameter would otherwise hide the field
}
public Account credit(int amount) {
this.balance += amount;
return this; // callers can chain: account.credit(10).credit(5)
}
public int balance() {
return balance;
}
public static void main(String[] args) {
Account a = new Account(100).credit(20);
System.out.println(a.balance()); // prints: 120
}
}class Logger {
void log(String msg) {
System.out.println("base: " + msg);
}
}
class PrefixLogger extends Logger {
@Override
void log(String msg) {
super.log("app " + msg); // run the parent version, not this override
}
public static void main(String[] args) {
new PrefixLogger().log("ready"); // prints: base: app ready
}
}Common Gotchas
`this` is unavailable in a static method
there is no current object. Passing instance state into a `static` method requires an explicit argument.
`super(...)` is not optional when the parent lacks a no-arg constructor
write it as the first statement.
`super.method()` skips the override on this class only
further subclasses still see their own overrides when the parent calls `this.method()` instead.
Inner classes need `Outer.this`
a shadowed enclosing instance is not the same as the inner `this`.