Menu

The super and this Keywords in Java

The super and this Keywords

As classes get more layered, with constructors calling other constructors and subclasses building on superclasses, you need a clear, unambiguous way to refer to 'the current object' versus 'my parent class'. Java gives you exactly two keywords for this: this and super.

Using this to Refer to Current Object

this is a reference to the object whose method or constructor is currently executing. Its most common use is resolving a naming clash between a field and a constructor or method parameter that share the same name.

class Rectangle {

int width;

int height;

Rectangle(int width, int height) {

this.width = width;  // this.width = the field, width = the parameter

this.height = height;

}

int area() {

return this. width * this. height;   // 'this.' is optional here, no clash exists

}

}

This can also be passed as an argument when a method needs to hand a reference to the current object over to another method or object, and it can be returned from a method to support method chaining, where multiple calls are written one after another on the same line.

class StringBuilderDemo {

private StringBuilder sb = new StringBuilder();

StringBuilderDemo append(String text) {

sb.append(text);

return this;  // returning the current object enables chaining

}

String build() {

return sb.toString();

}

}

String result = new StringBuilderDemo().append("Hello, ").append("World!").build();

Using super to Access Parent Class Members

super is the counterpart to this, but it points to the superclass rather than the current object. It is used when a subclass needs to reach a field or method from its parent that has been hidden or overridden by something with the same name in the subclass.

class Animal {

String sound = "Some generic sound";

void makeSound() {

System.out.println(sound);

}

}

class Dog extends Animal {

String sound = "Woof";   // hides (shadows) Animal's sound field

@Override

void makeSound() {

System.out.println(super.sound);   // explicitly reach Animal's field

System.out.println(this.sound);     // Dog's own field

super.makeSound();                   // call Animal's overridden version too

}

}

Calling super.makeSound() inside an overriding method is a common, deliberate pattern: it lets a subclass extend the parent's behaviour rather than fully replace it; the subclass runs the parent's logic first (or last), then adds its own.

Constructor Chaining with this() and super()

this() and super(), with parentheses, are special constructor calls, not field or method references. this(...) calls another constructor in the same class; super(...) calls a constructor in the immediate superclass. Either one, if used, must be the very first statement inside a constructor.

class Person {

String name;

Person(String name) {

this.name = name;

}

}

class Employee extends Person {

String company;

Employee(String name) {

this(name, "Unassigned");   // delegates to the constructor below

}

Employee(String name, String company) {

super(name);              // must run first — initialises Person's part

this.company = company;

}

}

If a constructor contains no explicit super(...) call at all, Java silently inserts a call to the superclass's no-argument constructor as the very first line. This is why removing your no-argument constructor from a superclass, once a subclass exists that does not call super(...) explicitly, can suddenly break compilation.

Keyword

Refers To

Typical Use

this

The current object

Resolving field/parameter naming clashes, method chaining, passing self as an argument

super

The immediate superclass

Accessing a hidden field, calling an overridden method's original version

this(...)

Another constructor in the same class

Avoiding duplicated initialisation logic across overloaded constructors

super(...)

A constructor in the superclass

Ensuring the inherited part of the object is properly initialised first

Did You Know?

this(...) and super(...) can never both appear in the same constructor, since each must be the first statement, and a constructor can only have one first statement. If you need both a custom superclass constructor call and shared initialisation logic, the usual fix is to put the shared logic in a separate helper method, called after super(...) runs.