Home >>Java Tutorial >This keyword in Java

This keyword in Java

This keyword in Java

this keyword in java is basically a reference of a variable that refers to the current object. There are numerous possible uses of the java this keyword.

Usage of java this keyword

As said earlier there are numerous use of the ‘this’ keyword in the Java language. Here are some of the important usages of java this keyword as depicted below:

  • this keyword in Java can generally be used in order to refer current class instance variable.
  • this keyword in Java can be used generally in order to invoke the current class method (implicitly)
  • this() keyword can basically be used in order to invoke the current class constructor.
  • this keyword is basically used to get passed as an argument in the method call in Java.
  • this keyword in Java can be used in order to pass them as argument in the constructor call.
  • this Keyword n Java can generally be used in order to return the current class instance from the method.
Here are ways that are depicted below to use the this keyword in java la language:

1. In this example we are using ‘this’ keyword in order to refer current class instance variables:

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

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

	public static void main(String[] args) 
	{ 
		Main object = new Main(10, 20); 
		object.display(); 
	} 
}
Output:
x = 10y = 20

2. In this example we are using this() to invoke current class constructor in Java:

// Java code for using this() to 
// invoke current class constructor 
class Main
{ 
	int x; 
	int y; 

	//Default constructor 
	Main() 
	{ 
		this(10, 20); 
		System.out.println("Inside default constructor \n"); 
	} 
	
	//Parameterized constructor 
	Main(int x, int y) 
	{ 
		this.x = x; 
		this.y = y; 
		System.out.println("Inside parameterized constructor"); 
	} 

	public static void main(String[] args) 
	{ 
		Main object = new Main(); 
	} 
}
Output:
Inside parameterized constructor
Inside default constructor

3. In this example we are using ‘this’ keyword in order to return the current class instance:

//Java code for using 'this' keyword 
//to return the current class instance 
class Main 
{ 
	int x; 
	int y; 

	//Default constructor 
	Main() 
	{ 
		x = 100; 
		y = 200; 
	} 
	
	//Method that returns current class instance 
	Main get() 
	{ 
		return this; 
	} 
	
	//Displaying value of variables a and b 
	void display() 
	{ 
		System.out.println("x = " + x + " y = " + y); 
	} 

	public static void main(String[] args) 
	{ 
		Main object = new Main(); 
		object.get().display(); 
	} 
}
Output:
x = 100 y = 200

4. In this example we are using ‘this’ keyword as a method parameter:

// Java code for using 'this' 
// keyword as method parameter 
class Main 
{ 
	int x; 
	int y; 
	
	// Default constructor 
	Main() 
	{ 
		x = 10; 
		y = 20; 
	} 
	
	// Method that receives 'this' keyword as parameter 
	void display(Main obj) 
	{ 
		System.out.println("x = " + x + "y = " + y); 
	} 

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

	public static void main(String[] args) 
	{ 
		Main object = new Main(); 
		object.get(); 
	} 
}
Output:
x = 10y = 20

5. In this example we are using ‘this’ keyword in order to invoke current class method in Java:

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

	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[]) { 
		Main t1 = new Main(); 
		t1.display(); 
	} 
}
Output:
Inside show funcion
Inside display function

6. In this example we are using ‘this’ keyword as an argument in the constructor call in Java:

// 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 = 5; 
	
	// 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 : 5