Java THIS Keywords

This keyword in java is used to refers the current object.This keyword is a reference variable in java programming language .
If a program that contain both instance variable and local variable with same name in this case we can use this keyword to represent the instance variable .
Note : We can not use this keyword inside the static area.
Example of this keyword 
class Test
{  
int a=100;      //Instance variable 
int b=200;     // Instance variable 
Void add(int a , int b)
{
System.out.println(a+b);
System.out.println(this.a+this.b); //Here use of this keyword 
}
public static void main(String[] args)
{  
Test t = new Test();   
t.add(400,500)
}
}

Output :  300
              900

This keyword usages in java

Important usages of java this keyword are as follows
  • This keyword used to refers the current class instance variables.
  • This Keyword is used to invoke the current class methods and constructor.
  • This keyword is used to passed an argument in methods and constructor call.
  • This keyword is used to return the current class instance form methods.

Use (1) : To refer current class instance variables use this keyword.
Syntax 
class Test
{  
int a=100;      //Instance variable 
int b=200;     // Instance variable 
Void add(int a , int b)
{
System.out.println(a+b);
System.out.println(this.a+this.b);  //Here use of this keyword
}
public static void main(String[] args)
{  
Test t = new Test();   
t.add(400,500)
}
}
Output :  300
              900
Explanation : 

Use (2) : To invoke the current class method use THIS keyword.
Syntax
class Test
{  
void add()
{
System.out.println("This is add method");
}  
void ref()
{  
System.out.println("This is ref method");  
this.m();  //Here use of this keyword
}  
}  
class TestResult
{  
public static void main(String args[])
{  
Test t=new Test();  
T.ref();  
}
}
Output  : This is ref method
              This is add method  

Use (3) To invoke the current class constructor use this keyword.

Rule : 1 -Call the constructor by this keyword only (constructor name not allowed like method).
         2- Inside the constructor this keyword must be first statement .
         3- one constructor is able to call only one constructor at a time , don't declare multiple this keyword inside a constructor. 
Syntax
class Test
{  
Test()
{
System.out.println("This is Constructor Block");
}  
Test(int x)
{  
this();  //Here use of this keyword
System.out.println(x);  
}  
}  
class TestResult
{  
public static void main(String args[])
{  
Test a=new Test(100);  

}
}
Output : This is Constructor Block
              100

Use (4) : To pass as an argument in the method use this keyword.

Syntax
class Test
{  
void m1(Test obj)
{  
System.out.println("method ");  
}  
void y()
{  
m1(this);  //Here use of this keyword
}  
public static void main(String args[])
{  
Test t = new Test();  
t.y();  
}  

Output : method

Use (5) To pass as an argument in the constructor call use this keyword.

Syntax 
class Test
{  
 Test1 obj;  
 Test(Test1 obj)
{  
this.obj=obj;  //Here use of this keyword
}  
void display()
{  
System.out.println(obj.data);  
}  
}  
  
class Test1{  
int data=100;  
Test1()
{  
Test t=new Test(this);  
t.display();  
}  
public static void main(String args[])
{  
Test1 a=new Test1();  
}  
}  
Output : 100

Important Points : THIS KEYWORD

  • 'THIS' Keywords in Java is a reference variable that refers to the current object.
  • Inside the static areas this keywords not allowed .
  • Call the constructor by this keyword only (constructor name not allowed like method).
  • one constructor is able to call only one constructor at a time , don't declare multiple this keyword inside a constructor. 


Hope !!! The Above lesson on java this keyword helpful for you...

Technology Team, 

UPCS 
UP Consultancy Services 

Tags: This keyword in java , use of this keyword in java , java this keyword , this keyword usages .

This keyword in Java with Example