Java this keyword | 6 Uses of this keyword in Java.

this in java is a reference variable that refers to the current object. The this reference to the current object is useful in situations where a local variable hides, or shadows, a field with the same name. As we know, it is illegal in java to declare two local variables with the same name inside the same or enclosing scopes. However, when a local variable has the same name as an instance variable, the local variable hides the instance variable. We can use this to resolve any namespace collisions that might occur between instance variable and local variable. for example:

class Box{
 int length, breadth, heieght;
 public void setDimension(int length, int breadth, int height){
  this.length = length;
  this.breadth = breadth;
  this.height = height;
 }
}




6 Uses of this keyword in Java

1. this can be used to refer the current class instance variable.
2. this can be used to invoke the current class constructor.
3. this can be used to invoke the current class method.
4. this can be passed as an argument in the method call.
5. this can be passed as an argument in the constructor call.
6. this can be used to return the current class instance from the method.
this keyword in java


1. Example of this to refer current class instance variable:

When a local variable has the same name as an instance variable, the local variable hides the instance variable. We can use this to resolve any namespace collisions that might occur between instance variable and local variable.

class thisExample{
	int a, b; //instance varibles.
	//parameterized constructor.
	thisExample(int a, int b){
		this.a = a;
		this.b = b;
	}
	void display(){
		System.out.println("Value of a is: " +a);
		System.out.println("Value of b is: " +b);
	}
	public static void main(String args[]){
		thisExample obj = new thisExample(10, 20);
		obj.display();
	}
	
}


Output:

Value of a is: 10
Value of b is: 20



2. Example of this to invoke current class method:

this keyword in java can be used to invoke the current class method. Let's understand this with an example.

class thisExample{
	void display(){
		int a = 10;
		System.out.println("Value of a is: " +a);
	}
	void show(){
		//calling display() function.
		this.display();
		int b = 20;
		System.out.println("Value of b is: " +b);
	}
	public static void main(String args[]){
		thisExample obj = new thisExample();
		obj.show();
	}
}


Output:

Value of a is: 10
Value of b is: 20




3. Example of this to invoke the current class constructor:

The this() constructor can be used to invoke the current class constructor. We can also use this() to reuse the constructor this technique is known as constructor chaining.

class thisExample{
	int a, b;
	//default constructor.
	 thisExample(){
		 this(10,20);
		 System.out.println("We are inside the default constructor.");
	 }
	 //parameterized constructor.
	 thisExample(int a, int b){
		 this.a = a;
		 this.b = b;
		 System.out.println("We are inside the parameterized constructor.");
	 }
	public static void main(String args[]){
		thisExample obj = new thisExample();
	}
}


Output:

We are inside the parameterized constructor.
We are inside the default constructor



4. Example of this to pass as an argument in the method call:

this keyword can be used to pass as an argument in the method call in order to call another method of the same class. Let's understand this with an example.

class thisExample{
	int a, b;
	//default constructor.
	thisExample(){
		a = 10;
		b = 20;
	}
	// method that recieves 'this' keyword
	// as a parameter.
	void display(thisExample obj){
		System.out.println("Value of a is: " +a);
		System.out.println("Value of b is: " +b);
	}
	// method that returns current class instance.
	void show(){
		display(this);
	}
	public static void main(String args[]){
		thisExample obj = new thisExample();
		obj.show();
	}
}


Output:

Value of a is: 10
Value of b is: 20



5. Example of this to pass as an argument in the constructor call:

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

class A{
	B obj;
	//Parameterized constructor with object of B  
    // as an argument. 
	A(B obj){
		this.obj = obj;
		//calling the show() method of class B.
		obj.show();
	}
}
class B{
	int x = 50;
	//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 display the value of x.
	void show(){
		System.out.println("The value of x in class B is: " +x);
	}
	public static void main(String args[]){
		B obj = new B();
	}
}


Output:
The value of x in class B is: 50.


6. Example of this to return current class instance:

We can use this to return the current class instance. In this case, we can return this keyword as a statement from the method. The return type of the method must be the class type. Let's understand this with an example.

class thisExample{
	int a, b;
	//default constructor.
	thisExample(){
		a = 10;
		b = 20;
	}
	//method that returns the current class instance.
	thisExample get(){
		return this;
	}
	// displaying the value of a and b.
	void display(){
		System.out.println("Value of a is: " +a);
		System.out.println("Value of b is: " +b);
	}
	public static void main(String args[]){
		thisExample obj = new thisExample();
		obj.get().display();
	}
}


Output:

Value of a is: 10.
Value of b is: 20.




You may also like these posts:

1. Basic data types in java with example.


Java this keyword | 6 Uses of this keyword in Java. Java this keyword | 6 Uses of this keyword in Java. Reviewed by Prashant Srivastava on December 24, 2019 Rating: 5

No comments:

Powered by Blogger.