dsa

Java this Keyword

In this article, we will learn about this keyword in Java, how and where to use them with the help of examples.

There can be a lot of usage of java this keyword. In java, this is a reference variable that refers to the current object.

java this keyword
java this keyword

Usage of java this keyword

Here is given the 6 usage of java this keyword.

1. Using ‘this’ keyword to refer current class instance variables

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.

//Java code for using 'this' keyword to 
//refer current class instance variables 
class Test 
{ 
	int a; 
	int b; 
	
	// Parameterized constructor 
	Test(int a, int b) 
	{ 
		this.a = a; 
		this.b = b; 
	} 

	void display() 
	{ 
		//Displaying value of variables a and b 
		System.out.println("a = " + a + " b = " + b); 
	} 

	public static void main(String[] args) 
	{ 
		Test object = new Test(12, 50); 
		object.display(); 
	} 
} 

output:
a = 12  b = 50
 

2. Using this() 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.

// Java code for using this() to  
// invoke current class constructor 
class Test 
{ 
    int a; 
    int b; 
  
    //Default constructor 
    Test() 
    {   
        this(140, 280); 
        System.out.println("Inside  default constructor \n"); 
    } 
      
    //Parameterized constructor 
    Test(int a, int b) 
    { 
        this.a = a; 
        this.b = b; 
        System.out.println("Inside parameterized constructor"); 
    } 
  
    public static void main(String[] args) 
    { 
        Test object = new Test(); 
    } 
} 
output:
Inside parameterized constructor
Inside  default constructor
 

3. Using ‘this’ keyword to return the 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).

//Java code for using 'this' keyword 
//to return the current class instance 
class Test 
{ 
	int a; 
	int b; 

	//Default constructor 
	Test() 
	{ 
		a = 150; 
		b = 280; 
	} 
	
	//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 = 150  b = 280
 

4. Using ‘this’ keyword as method parameter

The this keyword can also be passed as an paramenter in the method. It is mainly used in the event handling.
// Java code for using 'this' 
// keyword as method parameter 
class Test 
{ 
	int a; 
	int b; 
	
	// Default constructor 
	Test() 
	{ 
		a = 150; 
		b = 280; 
	} 
	
	// Method that receives 'this' keyword as parameter 
	void display(Test obj) 
	{ 
		System.out.println("a = " +obj.a + " b = " + obj.b); 
	} 

	// Method that returns current class instance 
	void get() 
	{ 
		display(this); 
	} 

	public static void main(String[] args) 
	{ 
		Test object = new Test(); 
		object.get(); 
	} 
} 

output:
a = 150  b = 280
 

5. Using ‘this’ keyword to invoke current class method

You may invoke the method of the current class by using the this keyword. If you don't use the this keyword, compiler automatically adds this keyword while invoking the method.

// Java code for using this to invoke current 
// class method 
class Test { 

	void display() 
	{ 
		// calling function show() 
		this.show(); 
	
	System.out.println("Inside display function"); 
	} 
	
	void show() { 
		System.out.println("Inside show funcion"); 
	} 
	

	public static void main(String args[]) { 
		Test t1 = new Test(); 
		t1.display(); 
	} 
} 


output:
Inside show funcion
Inside display function
 

6. Using ‘this’ keyword as an argument in the constructor call

We can pass the this keyword in the constructor also. It is useful if we have to use one object in multiple classes.

// Java code for using this as an argument in constructor 
// call 
// Class with object of Class B as its data member 
class A 
{ 
	B obj; 
	
	// Parameterized constructor with object of B 
	// as a parameter 
	A(B obj) 
	{ 
		this.obj = obj; 
		
	// calling display method of class B 
		obj.display(); 
	} 
	
} 

class B 
{ 
	int x = 65; 
	
	// Default Contructor that create a object of A 
	// with passing this as an argument in the 
// constructor 
	B() 
	{ 
		A obj = new A(this); 
	} 
	
	// method to show value of x 
	void display() 
	{ 
		System.out.println("Value of x in Class B : " + x); 
	} 
	
	public static void main(String[] args) { 
		B obj = new B(); 
	} 
} 

output:
Value of x in Class B : 65