Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Java this vs super: Key Differences and Usage

Understand the difference between this and super in Java, including field access, method calls, and constructor chaining, with practical examples.

~5 min readJava
#Java#this keyword#super keyword#inheritance
Diagram showing this and super references in a Java inheritance hierarchy

In Java, this and super are two keywords that often appear together in the same class, but they serve different purposes. this refers to the current instance, while super refers to the superclass portion of the current object. Understanding the difference between this and super is essential for writing clear, maintainable code when working with inheritance and constructors.

What this and super Actually Reference

this is a reference to the current object—the instance on which the method or constructor is being invoked. It lets you access instance fields, call instance methods, and pass the current object to other methods. super, on the other hand, is a reference to the parent class portion of the current object. It allows you to access members of the superclass that may be hidden or overridden in the subclass.

Both keywords are implicit references; you don't declare them. They can be used in instance methods, constructors, and instance initializer blocks. Static contexts (static methods or static initializer blocks) cannot use this or super because there is no current instance.

Using this for Field and Method Access

The most common use of this is to disambiguate between instance fields and constructor or method parameters with the same name. For example:

java
public class Person { private String name; public Person(String name) { this.name = name; // 'this.name' refers to the field, 'name' refers to the parameter } public void setName(String name) { this.name = name; } }

Without this, the assignment name = name would be a no-op, leaving the field unchanged. this also lets you call another constructor of the same class using this(...), which we'll cover in a later section.

Using super to Access Parent Members

super gives you access to the superclass's fields and methods, especially when they are hidden or overridden. For instance, if a subclass overrides a method but still wants to call the parent version, super.method() is required:

java
public class Animal { public void speak() { System.out.println("Some sound"); } } public class Dog extends Animal { @Override public void speak() { super.speak(); // calls Animal.speak() System.out.println("Woof"); } }

Similarly, super.field accesses a superclass field that is shadowed by a subclass field. This is less common because fields should typically be private, but it can be useful in legacy code or when working with protected fields.

Constructor Chaining with this() and super()

Both this() and super() are used in constructors to call another constructor. this() calls another constructor of the same class, while super() calls a constructor of the parent class. They must be the first statement in a constructor, and you cannot use both in the same constructor.

Consider this example:

java
public class Base { public Base() { System.out.println("Base constructor"); } } public class Derived extends Base { public Derived() { super(); // explicit call to Base() System.out.println("Derived constructor"); } }

If you omit super() and do not start with this(...), the compiler automatically inserts a call to the no-argument superclass constructor. If the superclass doesn't have a no-argument constructor, you must explicitly call super(...) with the appropriate arguments.

this() is useful for reducing constructor duplication. For example:

java
public class Point { private int x; private int y; public Point() { this(0, 0); // calls Point(int, int) } public Point(int x, int y) { this.x = x; this.y = y; } }

This pattern keeps default values in one place and avoids repeating field initialization logic.

Common Mistakes and Pitfalls

One frequent error is trying to use this or super in a static method. Since static methods belong to the class rather than an instance, the compiler rejects such usage. Another mistake is assuming super can access private fields or methods of the parent class. Private members are not visible to subclasses, so super won't help; you need a public or protected accessor.

Another pitfall is mixing up this and super in constructor calls. For example, calling this() and super() in the same constructor is illegal because each must be the first statement. Also, you cannot call a constructor of the same class and a parent class in one constructor; you must choose one chain.

A subtle issue arises when a superclass constructor calls an overridden method. During construction, the subclass fields are not yet initialized, so the overridden method may see default values. This is a known anti-pattern; avoid calling overridable methods from constructors.

Maintainability Considerations

The main consideration when choosing this or super is code clarity and design. Overusing this when there is no name conflict (for example, this.field when no parameter shadows the field) adds noise. Excessive super calls can be a sign of deep inheritance hierarchies that are hard to maintain.

Constructor chaining with this() reduces duplication but can make the flow harder to follow if overused. Keep constructor chains short and ensure each constructor has a clear responsibility. When overriding methods, decide whether the parent behavior should be preserved; calling super.method() is a deliberate choice that should be documented.

When to Use Which: Decision Guide

The choice between this and super depends on what you're trying to reference:

SituationUse thisUse super
Access current instance fieldYesNo
Access parent class fieldNoYes
Call current class methodYesNo
Call overridden parent methodNoYes
Call another constructor of same classYes (this(...))No
Call parent class constructorNoYes (super(...))
Pass current object to another methodYesNo

In practice, this is used far more often because most code operates on the current instance. super is reserved for specific scenarios: overriding methods that need parent behavior, accessing hidden fields, or calling a non-default superclass constructor.

A good rule of thumb: if you need to refer to the current object, use this. If you need to refer to the parent class's implementation, use super. Avoid using either when the reference is implicit—for example, calling a method on the current instance without a name conflict doesn't require this.

Advanced Usage: this in Method Chaining and Passing References

this is often used to return the current object from a method, enabling fluent interfaces. For example:

java
public class MessageBuilder { private StringBuilder content = new StringBuilder(); public MessageBuilder append(String text) { content.append(text); return this; } public String build() { return content.toString(); } }

this can also be passed as an argument to another method that expects the current object, such as registering a listener. super has no equivalent use; it cannot be passed as a reference because it is not a complete object—it's only the parent portion of the current object.

Inside an inner class, this can become confusing. this refers to the inner class instance, not the outer class. To access the outer instance, qualify it as OuterClass.this. This is distinct from the super keyword, which always refers to the immediate parent class of the current class.

Understanding these nuances helps you avoid subtle bugs, especially in complex inheritance hierarchies. Always test constructor chains and overridden methods to ensure the intended behavior is preserved.

Try matching browser tools

All tools

Small utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Java guides

More in Java

All Java guides

The guides filed next to this one in Java, in library order.