this keyword in Java

In this java programming tutorial, we will study and understand the working of this keyword in java. In simple terms – ‘this’ is a reference variable that refers to the current object. There are many different use cases of the this keyword in Java. Here are few use cases of the this keyword –

  • It can be used to refer current class instance variable.
  • this can be passed as an argument in the method call.
  • this keyword can be used to return current class instance.
  • this can be passed as argument in the constructor call.
  • this() : to invoke current class constructor.
1)  THIS keyword used to refer current class instance variable.

The this keyword can be used to refer current class instance variable. If there is ambiguity between the instance variables and parameters, this keyword resolves the problem of ambiguity. Lets take a program example –

this keyword program example
public class MyClass {
   
    int num;
    public void setNum(int num)
    {
        this.num = num;
    }
    
    public static void main(String[] args) {
        
        MyClass obj = new MyClass();
        obj.setNum(5);
        
        System.out.println("Num value : "+obj.num);

    }  
}
Output
Num value : 5
Program Explanation –

In this program writtern above, you can see that we have an instance variable of the class named num. Now in the parameter argument variable that we are passing in the setNum(int num) function is also named num. So in order to prevent ambiguity between which num is which variable, we use the this keyword. Thus the this.num is the instance variable and the num being passed as argument is simple an external variable value.

2)  THIS keyword passed as an argument in the method call.

The this keyword can also be passed as an argument in the method. It is mainly used in the event handling. Lets take a program example –

this keyword passed as an argument in the method call
public class MyClass {
   
    void function1(MyClass obj)
    {
        System.out.println("Function 1 invoked");
    }
    void function2()
    {
        function1(this);
    }
    
    public static void main(String[] args) {
        
        MyClass obj = new MyClass();
        obj.function2();

    }  
}
Output
Function 1 invoked
Program Explanation –

In the above written program we are calling function1 by using function2. To do so, we are passing the same object reference using the this keyword. the this keyword in this case is actually referring to the current MyClass object.

3)  THIS keyword can be used to return current class instance.

We can return this keyword as an statement from the method. In such case, return type of the method must be the class type (non-primitive). Lets see a program example for the same –

this keyword used to return current class instance.
class Test
{
    int a;
    int b;
 
    //Default constructor
    Test()
    {
        a = 10;
        b = 20;
    }
     
    //Method that returns current class instance
    Test get()
    {
        return this;
    }
     
    //Displaying value of variables a and b
    void display()
    {
        System.out.println("a = " + a + "  b = " + b);
    }
 
    public static void main(String[] args)
    {
        Test object = new Test();
        object.get().display();
    }
}
Output
a = 10 b = 20
Program Explanation –

In this program written above, we used this keyword to return the reference to the current object via the get() function and then accessed their instance variables.

4)  THIS keyword can be passed as argument in the constructor call.

Just like the this keyword can be used to refer current class instance variable in a method, it can also be used in a constructor to refer the instance variable. Let us see a program example –

this keyword passed as argument in the constructor call.
int num;
    
    MyClass(){}
    
    MyClass(int num)
    {
        this.num = num;
    }
    
    public static void main(String[] args) {
        
        MyClass obj = new MyClass(5);
        
        System.out.println("Num value : "+obj.num);

    }
Output
Num value : 5
Program Explanation –

As per the program we are resolving the ambiguity between the instance variable num and the arguement variable num being passing in the parameterized constructor and the distinction is being done by the this keyword. Also the this keyword can work with user defined class objects too.

5)  this() keyword is used to invoke current class constructor.

The this() constructor call can be used to invoke the current class constructor. It is used to reuse the constructor. In other words, it is used for constructor chaining. Lets see a program example –

this keyword used to invoke current class constructor.
class Student{  
int rollno;  
String name,course;  
float fee;  
Student(int rollno,String name,String course){  
this.rollno=rollno;  
this.name=name;  
this.course=course;  
}  
Student(int rollno,String name,String course,float fee){  
this(rollno,name,course);//reusing constructor  
this.fee=fee;  
}  
void display(){System.out.println(rollno+" "+name+" "+course+" "+fee);}  
}  
class TestThis7{  
public static void main(String args[]){  
Student s1=new Student(111,"ankit","java");  
Student s2=new Student(112,"sumit","java",6000f);  
s1.display();  
s2.display();  
}}
Output
111 ankit java null
112 sumit java 6000

Thus you can see that there are various applications and instances where the this keyword is used in Java.

Watch it on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *