Java this keyword

In java usage of this keyword is to refer the current object. We can also say it is a reference variable that refers to the current object.

Sometimes a method will need to refer to the object that invoked it. To allow this, Java defines the this keyword. this can be used inside any method to refer to the current object. i.e., this is always a reference to the object on which the method was invoked. We can use this anywhere a reference to an object of the current class type is permitted.

In java there are many usages of this keyword. Following is the usage of this keyword-

  • It is used to refer current class instance variable.
  • It is used to invoke current class constructor.
  • It is used to invoke current class method.
  • It can be passed in argument in the method call.
  • It can be passed in argument in the constructor call.
Examples
this. refer current class instance variable

this is used to refer to the existing class instance variable.

		class Employee{  
		int id;  
		String name;  
		int salary;  
		Employee(int id,String name,int salary){  
		this.id=id;  
		this.name=name;  
		this.salary=salary;  
		}  
		void display(){System.out.println(id+" "+name+" "+salary);}  
		}  

		class Newemployee{  
		public static void main(String args[]){  
		Employee e1=new Employee(101,"Gaurav",25000);  
		Employee e2=new Employee(102,"Saurav",30000);
		Employee e3=new Employee(103,"Neha",20000);
		e1.display();  
		e2.display();
		e3.display();
		}}  
Output

101 Gaurav 25000

102 Saurav 30000

103 Neha 20000

In the above example, we use this keyword to identify local and instance variable.

this() to invoke the current class constructor

The this() constructor is used to reuse the constructor from the constructor. It is used for chaining of the constructors.

		class Employee{  
		int id;  
		String name,designation;  
		int salary;  
		Employee(int id,String name,String designation){  
		this.id=id;  
		this.name=name;  
		this.designation=designation;  
		}  
		Employee(int id,String name,String designation,int salary){  
		this(id,name,designation);//reusing constructor  
		this.salary=salary;  
		}  
		void display(){System.out.println(id+" "+name+" "+designation+" "+salary);}  
		}  
		class Newemployee{  
		public static void main(String args[]){  
		Employee e1=new Employee(101,"Gaurav","Tester");  
		Employee e2=new Employee(102,"Saurav","Developer",30000);
		Employee e3=new Employee(103,"Neha","Designar",20000);
		e1.display();  
		e2.display();
		e3.display();
		}}  
Output

101 Gaurav Tester 0

102 Saurav Developer 30000

103 Neha Designer 20000

In below example, we can call default constructor from the parameterized constructor.
		class X{  
		X(){System.out.println("Hello Mr X");}  
		X(int A){  
		this();  
		System.out.println(A);  
		}  
		}  
		class Y{  
		public static void main(String args[]){  
		X x=new X(10);  
		}}  
Output

Hello Mr X

100

In below example, we can call a parameterized constructor from the default constructor.
		class X{  
		X(){  
		this(50);  
		System.out.println("Hello Mr X");  
		}  
		X(int a){  
		System.out.println(a);  
		}  
		}  
		class Y{  
		public static void main(String args[]){  
		X x=new X();  
		}}  
Output

50

Hello Mr X

this: to invoke a current class method

The this keyword is used to invoke the method of current class and if we cannot use this keyword then compiler automatically add this keyword while invoking the method.

Example
		class X{  
		void a(){System.out.println("Hello Mr X");}  
		void b(){  
		System.out.println("Hello Mr Y");  
		//a();//similar to this.a()  
		this.a();  
		}  
		}  
		class Demo{  
		public static void main(String args[]){  
		X x=new X();  
		x.b();  
		}}  
Output

Hello Mr Y

Hello Mr X

this: to pass argument in a method

The this keyword can be passed an argument in the method also. It is used in event handling.

Example
		class X{  
		  void a(X obj){  
		  System.out.println("method invoked");  
		  }  
		  void c(){  
		  a(this);  
		  }  
		  public static void main(String args[]){  
		  X x = new X();  
		  x.c();  
		  }  
		}   
Output

Method invoked

this: to pass argument in the constructor

this keyword can pass in constructor also when we have one object in multiple classes than it is used.

Example
		class XYZ{  
		  P obj;  
		  XYZ(P obj){  
		    this.obj=obj;  
		  }  
		  void display(){  
		    System.out.println(obj.data);//Here use data member of P class  
		  }  
		}  

		class P{  
		  int data=1000;  
		  P(){  
		   XYZ b=new XYZ(this);  
		   b.display();  
		  }
		  public static void main(String args[]){  
		   P p=new P();  
		  }  
		}    
Output

1000

Copyright 1999- Ducat Creative, All rights reserved.