Java this Keyword

In this article, you'll learn about this keyword in Java; how and where they are used with the help of examples.

this Keyword

In Java, this refers to the current object inside methods or constructors. Let's take an example to prove it.

class MyClass {
    int instVar;

    MyClass(int instVar){
        this.instVar = instVar;
        System.out.println("this reference = " + this);
    }

    public static void main(String[] args) {
        MyClass obj = new MyClass(8);
        System.out.println("object reference = " + obj);
    }
}

When you run the program, the output will be:

this reference = com.ThisAndThat.MyClass@74a14482
object reference = com.ThisAndThat.MyClass@74a14482

Notice that the object id of obj and this is same. Meaning, this is nothing but the reference to the current object.

There are 3 situations where this keyword are commonly used.


1. Using this to Disambiguate Variable References

In Java, it is not allowed to declare two or more variables having same name inside a scope (class scope or method scope). However, instance variables and parameters may have same name. Like this:

class MyClass {
    int instVar; // instVar instance variable

    MyClass(int instVar){ // instVar parameter
        instVar = instVar;
    }
}

In the above program, the Java compiler is confused due to name ambiguity. Hence, to address this problem, this keyword is used.

First, let’s see an example without using this keyword:

class MyClass {

    int instVar;
    MyClass(int instVar){
        instVar = instVar;
    }

    public static void main(String[] args) {
        MyClass mc = new MyClass(8);
        System.out.println("mc.instVar = " + mc.instVar);
    }
}

When you run the program, the output will be:

mc.instVar = 0

You might have expected 8 as an output, but, instead you get 0. This is because the Java compiler gets confused because of the ambiguity in names between the instance variable and the constructor parameter.

Now, let’s rewrite the above code and use this keyword to solve this issue.

class MyClass{

    int instVar;

    MyClass(int instVar){
        this.instVar = instVar;
    }

    public static void main(String[] args) {
        MyClass obj = new MyClass(8);
        System.out.println("obj.instVar = " + obj.instVar);
    }
}

When you run the program, the output will be:

mc.instVar = 8

Now, you’ll get the expected output. It’s because when you are create an object, the Java compiler knows which object has invoked the constructor.

When Java compiler invokes the constructor, this inside the constructor is replaced by the object which called the constructor.


Note: If you pass a parameter that has different name than that of instance variables, the compiler automatically appends this keyword.

This code

class MyClass {
    int instVar;

    MyClass(int i) {
        instVar = i;
    }
}

is equivalent to:

class MyClass {
    int instVar;

    MyClass(int i) {
        this.instVar = i;
    }
}

Another common use of this keyword is in setters and getters methods of a class. For example:

class POJO {
    String name;

    void setName( String name ) {
        this.name = name;
    }

    String getName(){
        return this.name;
    }

    public static void main( String[] args ) {
        POJO pojo = new POJO();
        pojo.setName("Toshiba");
        System.out.println("pojo.name: "+pojo.getName());
    }
}

When you run the program, the output will be:

pojo.name: Toshiba

2. Using this in Constructor Overloading

While working with constructor overloading, you may find it useful to invoke one constructor from another constructor. But, constructors can not be called explicitly. Hence, to accomplish this, you can use another form of this keyword this().

The pseudocode is shown here:

this(arg-list)

Here's how you can call a constructor from another constructor using this.

class Complex {

    private int a, b;

    // parameterize constructor
    private Complex( int i, int j ){
        this.a = i;
        this.b = j;
    }

    private Complex(int i){
        this(i, i); // invokes Complex(int i, int j);
    }

    private Complex(){
        this(0); // invokes Complex(int i);
    }

    @Override
    public String toString(){
        return this.a + " + " + this.b + "i";
    }

    public static void main( String[] args ) {
        Complex c1 = new Complex(2, 3);
        Complex c2 = new Complex(3);
        Complex c3 = new Complex();
        System.out.println(c1);
        System.out.println(c2);
        System.out.println(c3);
    }
}

When you run the program, the output will be:

2 + 3i
3 + 3i
0 + 0i

In the above program, no matter which constructor is called during the object instantiation, the parameterized constructor will be called eventually.


You must be careful while using this(). Constructors that call this() executes slow because calling another overloaded constructor adds overhead. If your class is used to create only handful of objects, then using this() is fruitful. Another huge advantage of using this() is to reduce the amount of duplicate code.

By the way, invoking one constructor from another constructor is called explicit constructor invocation.


3. Passing this as an Argument

If you need to pass the current object as an argument to a method, you can use this.

class ThisExample {

    int x;
    int y;

    ThisExample(int x, int y) {
        this.x = x;
        this.y = y;
        System.out.println("Before passing this to addTwo() method:");
        System.out.println("x = " + this.x + ", y = " + this.y);
        addTwo(this);
        System.out.println("After passing this to addTwo() method:");
        System.out.println("x = " + this.x + ", y = " + this.y);
    }

    void addTwo(ThisExample o){
        o.x += 2;
        o.y += 2;
    }
}

class Demo {
    public static void main( String[] args ) {
        ThisExample obj = new ThisExample(1, -2);
    }
}

When you run the program, the output will be:

Before passing this to addTwo() method:
x = 1, y = -2
After passing this to addTwo() method:
x = 3, y = 0

Here, addTwo() method is called with this as an argument from inside the constructor.