Skip to content

This Keyword

In Java, the "this" keyword is like a pointer that refers to the current object instance within a class. It's your trusty guide, helping you navigate through the maze of objects and methods with ease

Referring to Current Class Instance Variables

The "this" keyword can be used to refer to instance variables of the current class. It helps distinguish between instance variables and local variables with the same name.

java
public class Person {
    String name;
    
    public void setName(String name) {
        this.name = name; // Referring to instance variable
    }
}

Invoking Current Class Method (Implicitly)😗*

Inside a method, "this" can be used to invoke another method of the same class. It's like calling a friend by their first name – you don't need to specify who you're talking to!

java
public class Calculator {
    public void add(int a, int b) {
        int sum = this.sum(a, b); // Invoking another method
        System.out.println("Sum: " + sum);
    }

    private int sum(int x, int y) {
        return x + y;
    }
}

Using "this()" to Invoke Current Class Constructor

The "this()" syntax is used to invoke a constructor from another constructor within the same class. It allows for constructor chaining, enabling you to reuse initialization logic.

java
public class Car {
    String model;
    int year;

    public Car(String model) {
        this(model, 2022); // Invoking another constructor
    }

    public Car(String model, int year) {
        this.model = model;
        this.year = year;
    }
}

NOTE

when using the 'this' keyword to invoke another constructor within a class, the 'this' constructor call must always be the first line in the constructor body. This ensures proper initialization and avoids potential conflicts with other constructor logic

Passing "this" as an Argument in Method Call

You can pass the current object instance ("this") as an argument to a method. It's like sending a postcard with your address on it – the method knows where to find you!

java
public class Person {
    public void greet(Person friend) {
        System.out.println("Hello, " + friend.getName());
    }

    public String getName() {
        return this.name; // Returning instance variable
    }
}

Passing "this" as an Argument in Constructor Call

Similarly, you can pass "this" as an argument to another constructor when creating a new object. It's like introducing yourself to someone new – you pass along your identity!

java
public class Address {
    String city;
    String country;

    public Address() {
        this("Unknown", "Unknown"); // Passing "this" to another constructor
    }

    public Address(String city, String country) {
        this.city = city;
        this.country = country;
    }
}

Using "this" to Return the Current Class Instance from a Method

In a method, "this" can be used to return the current object instance. It's like saying, "Here I am!" – you announce your presence loud and clear!

java
public class Person {
    public Person getSelf() {
        return this; // Returning current instance
    }
}

Tabulating the Usages of "this" Keyword:

UsageExample
Referring to Current Class Instance Variablesthis.name = name;
Invoking Current Class Method (Implicitly)int sum = this.sum(a, b);
Using "this()" to Invoke Current Class Constructorthis(model, 2022);
Passing "this" as an Argument in Method CallSystem.out.println("Hello, " + friend.getName());
Passing "this" as an Argument in Constructor Callthis("Unknown", "Unknown");
Using "this" to Return Current Class Instancereturn this;

Waytojava is designed to make learning easier. We simplify examples for better understanding. We regularly check tutorials, references, and examples to correct errors, but it's important to remember that humans can make mistakes.