Codectionary / Developer documentation / Java

this and super Keywords

The 'this' keyword refers to the current object instance, most often used to distinguish between a field and a constructor/method parameter that share the same name, or to chain constructors. The 'super' keyword refers to the parent class, used to call the parent's constructor, access a parent field, or invoke a parent method that has been overridden.

Syntax

this.field = value;\nsuper.method();

Examples

Using this to Resolve Naming Conflicts

Distinguishing a field from a same-named constructor parameter.

public class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;  // this.name is the field, name is the parameter
        this.age = age;
    }
}

Using this to Chain Constructors

Calling another constructor in the same class.

public class Box {
    private int width, height;

    public Box(int size) {
        this(size, size);  // calls the two-argument constructor below
    }

    public Box(int width, int height) {
        this.width = width;
        this.height = height;
    }
}

Box square = new Box(5);       // 5x5
Box rect = new Box(4, 8);       // 4x8

Using super to Call the Parent Constructor

A subclass constructor typically calls super() to initialize inherited fields.

class Animal {
    protected String name;

    public Animal(String name) {
        this.name = name;
    }
}

class Dog extends Animal {
    private String breed;

    public Dog(String name, String breed) {
        super(name);  // must be the first line in the subclass constructor
        this.breed = breed;
    }
}

Dog dog = new Dog("Rex", "Labrador");

Using super to Access an Overridden Method

Calling the parent's version of a method that the subclass has overridden.

class Vehicle {
    public String describe() {
        return "A vehicle";
    }
}

class Car extends Vehicle {
    @Override
    public String describe() {
        return super.describe() + " that is a car";  // extends, doesn't replace
    }
}

Car car = new Car();
System.out.println(car.describe());  // A vehicle that is a car

Best practices

  • Use this. to disambiguate between a field and a parameter of the same name, a very common and idiomatic pattern in constructors
  • A call to super(...) must be the very first statement in a subclass constructor - Java enforces this at compile time
  • Use super.methodName() when a subclass override needs to extend the parent behavior rather than completely replace it
  • You don't need this. for every field reference - use it specifically when there's a naming conflict or for clarity in constructors

At a glance

Purpose
General-purpose application development
File extension
.java
Runs in
Java Virtual Machine
Usually used with
JDK and Java libraries

Specifications & further reading

Related Java documentation

Classes
Classes in Java are blueprints for creating objects, defining their properties (fields) and behaviors (methods). Java is a strictly object-oriented language where everything is encapsulated within classes. A class serves as a template that specifies what data an object will store and what operations it can perform.
Interfaces
An interface defines a contract of methods that implementing classes must provide, without specifying how those methods work internally. Unlike a class, an interface cannot be instantiated directly - it only declares what a class can do, not how. A class uses the 'implements' keyword to fulfill an interface's contract, and a single class can implement multiple interfaces, which is Java's way of achieving a form of multiple inheritance.
Abstract Classes
An abstract class is a class that cannot be instantiated directly and may contain both fully implemented methods and abstract methods (declared without a body, which subclasses must implement). Abstract classes sit between interfaces and regular classes: like an interface, they define a contract; like a regular class, they can hold state (fields) and provide shared, already-implemented behavior.
Constructors
A constructor is a special method that runs automatically when an object is created with 'new', typically used to initialize the object's fields. A constructor shares its name with the class and has no return type, not even void. Java supports constructor overloading, letting a class offer multiple ways to construct an object, and constructor chaining with this(...), where one constructor calls another in the same class.