Java this and super Keywords – Complete Guide with Examples


Learn the purpose and usage of this and super keywords in Java, including examples for constructors, methods, and inheritance, to write clean and reusable object-oriented code.

this and super Keywords in Java – Complete Detailed Tutorial

Java provides two special keywordsthis and super — to refer to objects and members in classes and inheritance hierarchies.

1. this Keyword in Java

The this keyword refers to the current object of a class. It is commonly used for:

  1. Referencing instance variables
  2. Calling another constructor (constructor chaining)
  3. Passing the current object as a parameter
  4. Invoking current class methods

1.1 Referencing Instance Variables

  1. Used when local variable and instance variable have the same name

class Student {
String name;
int age;

Student(String name, int age) {
this.name = name; // refers to instance variable
this.age = age;
}

void display() {
System.out.println("Name: " + this.name + ", Age: " + this.age);
}
}

public class Main {
public static void main(String[] args) {
Student s1 = new Student("Chinmaya", 25);
s1.display();
}
}

Output:


Name: Chinmaya, Age: 25

1.2 Constructor Chaining

  1. Call one constructor from another using this()

class Student {
String name;
int age;

Student() { // default constructor
this("Unknown", 18); // calls parameterized constructor
}

Student(String name, int age) {
this.name = name;
this.age = age;
}

void display() {
System.out.println("Name: " + name + ", Age: " + age);
}
}

public class Main {
public static void main(String[] args) {
Student s1 = new Student();
s1.display();
}
}

Output:


Name: Unknown, Age: 18

1.3 Passing Current Object as Parameter


class Printer {
void print(Student s) {
System.out.println("Printing Student: " + s.name);
}
}

class Student {
String name;

Student(String name) {
this.name = name;
}

void show() {
Printer p = new Printer();
p.print(this); // pass current object
}
}

public class Main {
public static void main(String[] args) {
Student s1 = new Student("Chinmaya");
s1.show();
}
}

Output:


Printing Student: Chinmaya

1.4 Invoking Current Class Method


class Student {
void greet() {
System.out.println("Hello from greet()");
}

void show() {
this.greet(); // calls current class method
}
}

public class Main {
public static void main(String[] args) {
Student s = new Student();
s.show();
}
}

Output:


Hello from greet()

2. super Keyword in Java

The super keyword refers to the parent class (superclass). It is used for:

  1. Accessing parent class methods
  2. Accessing parent class variables
  3. Calling parent class constructor

2.1 Accessing Parent Class Method


class Animal {
void eat() {
System.out.println("Animal is eating");
}
}

class Dog extends Animal {
void eat() {
System.out.println("Dog is eating");
}

void show() {
super.eat(); // call parent class method
eat(); // call current class method
}
}

public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.show();
}
}

Output:


Animal is eating
Dog is eating

2.2 Accessing Parent Class Variable


class Animal {
String color = "White";
}

class Dog extends Animal {
String color = "Black";

void printColor() {
System.out.println(color); // child class variable
System.out.println(super.color); // parent class variable
}
}

public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.printColor();
}
}

Output:


Black
White

2.3 Calling Parent Class Constructor


class Animal {
Animal() {
System.out.println("Animal Constructor");
}
}

class Dog extends Animal {
Dog() {
super(); // calls parent class constructor
System.out.println("Dog Constructor");
}
}

public class Main {
public static void main(String[] args) {
Dog d = new Dog();
}
}

Output:


Animal Constructor
Dog Constructor

3. Summary

KeywordPurpose
thisRefers to current object, used for variables, methods, constructor chaining, passing object
superRefers to parent class, used for variables, methods, and constructors

Key Points:

  1. this() → calls current class constructor
  2. super() → calls parent class constructor
  3. this is optional, but improves readability and clarity