1. "this" in java

In java, each non-static method and constructor has an implicit reference variable by the name "this". It receives the reference of the invoking object when the method or constructor is invoked.

Note: "this" keyword cannot be used in static methods in java.

2. Uses of "this" in java

This keyword can be used in the following ways:

2.1. To refer data members of the invoking object

It is used to refer to data members of the invoking object in case there is a conflict between data members and local variables.

The following example demonstrates the use of "this". There is a conflict between data members and local variables of the constructor because of this conflict data members cannot be directly referenced in the constructor, they can only be referred with the help of this keyword.

class Test{
    private int num1, num2;
    
    public Test(int num1, int num2){
        num1 = num1;
        num2 = num2;
    }
	
    public void display(){
	System.out.println("num1 = "+num1);
	System.out.println("num2 = "+num2);
    }
    
    public static void main(String args[]) {
        Test obj = new Test(4,8);
	obj.display();
    }
}

Output:

num1 = 0
num2 = 0

This is not going to work because the num1 and num2 variables of the invoking object are never referenced in the constructor. So the values are never assigned to them. To make this work we have to use this in the constructor to refer to the variables of the invoking object. Modify the constructor by adding this keyword.

public Test(int num1, int num2){
	this.num1 = num1;
	this.num2 = num2;
}

Output:

num1 = 4
num2 = 8

Note: this can also be used in the display method to refer to the num1 and num2 variables of invoking object if there was a conflict between variables.

2.2. For intra-class constructor chaining

this can be used for intra-class constructor chaining.

Intra-class constructor chaining: The facility of invoking a constructor from another constructor of the same class is called intra-class constructor chaining.

The following example demonstrates how to chain the constructors of the same class together using this 

class Test{
     private int num1, num2;
	 
     public Test(){
	this(2,3);
	System.out.println("In default constructor");
    }
	
    public Test(int num1){
	this(num1,3);
	System.out.println("In one parameterized constructor");
    }
    
    public Test(int num1, int num2){
	this.num1 = num1;
	this.num2 = num2;
	System.out.println("In two parameterized constructor");
    }
    
    public static void main(String args[]) {
	System.out.println("----Creating objA----");
        Test objA = new Test();
	System.out.println("----Creating objB----");
	Test objB = new Test(10);
	System.out.println("----Creating objC----");
	Test objC = new Test(20,30);
    }
}

Output:

----Creating objA----
In two parameterized constructor
In default constructor
----Creating objB----
In two parameterized constructor
In one parameterized constructor
----Creating objC----
In two parameterized constructor

As seen above, the default and the one parameterized constructor are chained with the two parameterized constructor. That's why two parameterized constructor was invoked while creating objA and objB.

Note: To facilitate constructor chaining this() must be the first statement of a constructor.

2.3. For method chaining

Just like constructor chaining methods can also be chained together using this in java.

Example:

class Test{
    private int num1, num2;
	
    public Test m1(){
	System.out.println("m1 is invoked");
	// do something
	return this;
    }
	
    public Test m2(){
	System.out.println("m2 is invoked");
	// do something
	return this;
    }
    
    public Test m3(){
	System.out.println("m3 is invoked");
	// do something
	return this;
    }
    
    public static void main(String args[]) {
	new Test().m1().m2().m3();
    }
}

Output:

m1 is invoked
m2 is invoked
m3 is invoked

For method chaining, every method should return this.

Note: All constructors implicitly returns this that's why the call to constructor was also chained with the methods.

3. Conclusion

this can be very useful if used properly in java programming. It provides a lot of features which can be used to make our life a little easier.