Java this Keyword Complete Tutorial

In this section, we will learn what the `this` keyword is and how it works in Java.

What is `this` in Java?

In short, the `this` keyword means: the member that belongs to the current instance object that invoked the call.

Take a look at the class below:

public class Person {
    private String name;
    private String lastName;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        name = name;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        lastName = lastName;
    }
}

Inside the `setLastName()` we have an assignment which is:

lastName = lastName;

The question is: which variable is pointing towards the attribute of the class and which variable is pointing towards the local variable of this method?

Let’s say it this way: which of these two variables has class scope and which one of them has Method scope?

When we design a method this way, we can’t differentiate between these variables! Even the compiler will return error because it can’t differentiate between them too!

This is where the keyword `this` comes in.

Basically, when we attach this keyword to a variable inside a method, it will tell the compiler that the point of the variable is towards the one that is declared with Class scope (as the attribute of the class) and not the one that is declared inside the method.

Use of `this` keyword in Java

There are multiple places we can use the `this` keyword.

Now in the sections below we will see the areas that we’re allowed to use the `this` keyword as well as how to use it.

Using `this` to invoke current object variable (field)

One of the places where we can use the `this` keyword is within the body of methods! This is especially useful when we have the fields of a class having the same name as the parameters of the target method.

Using the `this` keyword, we can attach the keyword to the name of the field in the class, which means the target variable belongs to the object that invoked the call and not the parameter of that method!

Example: invoking current object variable (field) via `this`

Let’s refactor the last example to see how the `this` keyword can be used:

public class Person {
    private String name;
    private String lastName;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

Here the `this.lastName` inside the `setLastName()` method is pointing towards the variable that is declared as the attribute of the class.

Also inside the `setName()` method, the ` this.name` is pointing towards the variable `name` that is declared as the attribute of the class and not the one that is the local variable of the `setName()` method.

Using `this` to invoke current object method

Other than the fields of an object, we can use the `this` keyword to invoke a method of an object as well!

Note: you seldom need to call a method of an object using the `this` keyword.

Example: invoking current object method via `this`

class Main{

    private String name; 
    private String lastName;

    public Main(String name , String lastName){
        this.name = name; 
        this.lastName = lastName;
    }

    public static void main(String[] args) {
        Main main = new Main("John","Doe");
        main.needObj();
    }
    public void printName(){
        System.out.println(this.name+" "+this.lastName);
    }
    public void needObj(){
        this.printName();
    }
   
}

Output:

John Doe

Using `this` to return the current object

The `this` keyword can be used as the return value of a method as well. In such case, the return value `this` is a pointer to the object that invoked the target method.

Note: returning the `this` keyword will only return a reference of the target object and not a copy of it!

Example: returning current object via `this`

class Main{

    private String name; 
    private String lastName;

    public Main(String name , String lastName){
        this.name = name; 
        this.lastName = lastName;
    }

    public static void main(String[] args) {
        Main main = new Main("John","Doe");
        Main refMain = main.returnMySelf();
        refMain.printName();

    }
    public void printName(){
        System.out.println(this.name+" "+this.lastName);
    }
    public Main returnMySelf(){
        return this; 
    }
   
}

Output:

John Doe

Using `this` to pass the current object as the argument a method call

If for whatever reason a method needed an object of the same class type for its parameter, we can use the `this` keyword as the argument as well. This is because, after all, the `this` keyword is a reference to the object that invoked the call.

Example: passing `this` as an argument to a method calls

class Main{

    private String name; 
    private String lastName;

    public Main(String name , String lastName){
        this.name = name; 
        this.lastName = lastName;
    }

    public static void main(String[] args) {
        Main main = new Main("John","Doe");
        main.needObj();
    }
    public void printName(Main main){
        System.out.println(main.name+" "+main.lastName);
    }
    public void needObj(){
        printName(this);
    }
   
}

Output:

John Doe

In the body of the `main` method, take a look at this statement:

main.needObj();

Here the `main` object is the caller of the `needObj()` method. So inside the body of the `needObj` the `this` keyword will refer to the `main` object for this specific call.

Now take a look at the body of the `needObject`.

Specifically, this statement: printName(this);

The `printName()` method needs an object of type `Main` class but we gave the value `this` as its argument. This is because the `this` keyword is equal to the `main` object (again because the call to the `needObj()` happened using the `main` object). Basically, that means we’re passing a reference of the `main` object to the `printName()` method.

Using `this` in object constructor

Besides the locations mentioned above, we can use the `this` keyword in the constructors as well. This is covered in later sections.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies