The this keyword can be very confusing for beginners because we don’t know what is the this keyword referring to. In this topic we’ll dicuss the use of this keyword in Java.

The uses of this keyword

  • It can be use to refer to the current instance class variables
public class MyClassOne {
    String name;
    public MyClassOne(String name) {
        this.name = name; // the this.name refer to the name variable and not the name argument.

        name = name; // not gonna work, you are passing the argument name to itself.

    }
}
Output:
CodeAndWave

In the first line of the code above, the this keyword in the this.name refers to the instance variable and not the name argument.

In the second line, it is not gonna work because you are passing the name parameter to itself.

  • It can be used to call current class constructor.

We can do this by using this()

public class MyClassTwo {

    String name;

    public MyClassTwo() {
        this("CodeAndWave - From default Constructor");
    }

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

    void print(){
        System.out.println(name);
    }
}

NOTE: The Call to this() must be the first statement in constructor. Meaning if you call another this() with different signature the compiler won't allow you.

See the constructor with no argument? We call the this keyword and then pass a string argument. The compiler then will find a constructor that matches the signature and then call that constructor.


  • It can be pass as an argument to a method
public class MyClassOne {

    String name;

    public MyClassOne(String name) {
        this.name = name; // the this.name refer to the name variable and not the name argument

    }

    void print(){
        System.out.println(name);
    }

    public void printFromMyClassThree(){
        MyClassThree myClassThree = new MyClassThree();
        myClassThree.printMyClassOne(this); // see the 'this' keyword here? We use the it to pass the instance of the current class

                                            // the 'this' keyword here is referring to the MyClassOne object

    }
}

public class MyClassThree {

    public void printMyClassOne(MyClassOne myClassOne){
        myClassOne.print(); // see this? we call the print() function of MyClassOne

    }
}

public class Main {

    public static void main(String[] args) {
        MyClassOne myClassOne = new MyClassOne("CodeAndWave");
        myClassOne.printFromMyClassThree();
    }
}
Output:
CodeAndWave

Take a look at the code carefully, read it, and try it on your IDE. This is the only way you could understand what is happening here. Like I said, the this keyword can be confusing sometimes.


  • It can be use to return class instance state
public class Person {
        String name;
        int age;
    public Person() {
        this("CodeAndWave",20); // the this here is referring to the constructor that matches its signature

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

    Person getPersonInfo() {

        return  this; // the this here is referring to current instance class

     }

}

public class Main {
    
    public static void main(String[] args) {
        Person p = new Person();
        System.out.println("Hi I am "+p.getPersonInfo().name + "  and my age is " + p.getPersonInfo().age);
    }
}
Output:
Hi I am CodeAndWave  and my age is 20

Take your time and read the code carefully cause it is a bit confusing.

The first this on the default constructor

this("CodeAndWave",20);
is referring to the constructor that matches its signature(the second constructor).

The second this keyword

Person getPersonInfo() {
        return  this; // the this here is referring to current instance class

     }
is referring to the current object(the current instance class)


Hopefully you understand even just a bit about how the this keyword works. I know it is confusing so just take your time. Try it on your IDE. Run it and mess with it. Thats the best way to understand this this keyword. HA! See what I did there? this "this".