wellnwill Marathahalli Bangalore

this keyword


this is a reference variable and represents current class instance (object).

Usage of this keyword

  • this keyword can be used to access non static variables of current class instance.
  • this keyword can be used to call current class method.
  • this() can be used to call current class constructor with in a constructor.
  • Call to this must be first statement in constructor.

Case1:this keyword can be used to access non static variables of current class instance.

We can see below problem without using this keyword.

class Bulb
 {  
  int watt;  
  String color;
  float price; 
  
// The value which are received while constructor calling
   will be reflected within constructor block only. 
   The values of object will not be changed.
  
  Bulb(int watt, String color, float price)
  {   
   watt=watt;  
   color=color;  
   price=price;
  System.out.println("Bulb is created with default values");
  } 
  
void showValue()
  {
  System.out.println("display data");  
  System.out.println("watt :"+ watt);
  System.out.println("color :"+ color);
  System.out.println("price :"+ price);
  } 
public static void main(String args[]){
  //constructor calling	
  Bulb b1=new Bulb(100,"white",15.50F);  
  b1.showValue();  
 }  
}
output:
Bulb is created with default values
display data
watt :0
color :null
price :0.0

Solution with this keyword

The issue what we are facing in pevious program will be resolved by this keyword.

class Bulb
 {  
  int watt;  
  String color;
  float price; 
  
  Bulb(int watt, String color, float price)
  {   
   this.watt=watt;   // this.watt is referring to the current object’s instance variable i.e. b1.watt
   this.color=color;  
   this.price=price;
   System.out.println("Bulb is created with actual values");
  }  
 
  void showValue()
  {
  System.out.println("display data");  
  System.out.println("watt :"+ watt);
  System.out.println("color :"+ color);
  System.out.println("price :"+ price);
  } 
  
 public static void main(String args[]){  
  Bulb b1=new Bulb(100,"white",15.50F);  //constructor calling
  b1.showValue();  
 }  
}
// O/P-
Bulb is created with actual values
display data
watt :100
color :white
price :15.5

Where is this keyword not required

If local variables (formal arguments) and instance variables have different name then there is no need to use this keyword. We can consider the below example to understand this point.

class Bulb{  
 int watt;
 float price;
 String color;
 boolean state;
  
  Bulb(int w, float p, boolean b)
  {
   watt=w;
   price=p;
   state=b;
  }
  
  Bulb(int w, float p, boolean b, String clr)
  {
   this(w,p,b);
   color=clr;
  }
  
 
void displayDetails()
 {
 System.out.println("watt: "+watt);
 System.out.println("price: "+price);
 System.out.println("state: "+state);
 System.out.println("color: "+color);
 }
} 

class Test158{  
 public static void main(String args[]){ 
  
  Bulb b1=new Bulb(100,15.50f,true); 
  Bulb b2=new Bulb(200,25.75f,false,"red"); 
  System.out.println("-----Details of Bulb b1-----");
  b1.displayDetails();
  System.out.println("-----Details of Bulb b2-----");
  b2.displayDetails();
 }  
}  

Case 2:this keyword can be used to call current class method

class Bulb{  
 int watt;  
 String color;
 float price; 
 
 void createBulb(int w)
 {   
  watt=w;  
 }  
  
 void createBulb(int w, float p)
 {   
  this.createBulb(w);  
  price=p;
 }  
 
  void createBulb(int w,String c,float p)
 {   
  this.createBulb(w,p);  
  color=c;  
 } 
void displayDetails()
 {
 System.out.println("watt: "+watt);
 System.out.println("price: "+price);
 System.out.println("color: "+color);
 }
}

class Test159
{  
 public static void main(String args[])
 {  
  Bulb b1=new Bulb();  
  b1.createBulb(100,"white",15.50F);
  b1.displayDetails();  
 }  
}  





We can use this keyword to call the method of current class.

Suppose while calling current class method. we are not using this keyword then compiler will add this keyword while calling that method. Consider the below program.


  • this() can be used to call current class constructor with in a constructor.
  • We can call one constructor from another constructor using this(). This process is known as constructor chaining.
  • The advantage of this approach is to reuse the functionality of existing constructors inside a class.
class Bulb{  
 int watt;  
 String color;
 float price; 
 
 Bulb(int w)
 {   
  watt=w;  
 }  
  
 Bulb(int w, float p)
 {   
  this(w);  
  price=p;
 }  
 
 
 Bulb(int w,String c,float p)
 {   
  this(w,p);  
  color=c;  
 } 
 
 void displayDetails()
 {
 System.out.println("watt: "+watt);
 System.out.println("price: "+price);
 System.out.println("color: "+color);
 }
}  
class Test189
{  
 public static void main(String args[]){  
  Bulb b1=new Bulb(100,"white",15.50F);  
  Bulb b2=new Bulb(200,25.5f);
  Bulb b3=new Bulb(300);
  b1.displayDetails();  
  b2.displayDetails();
  b3.displayDetails();
 }  
}
class Bulb
{  

void createBulb()
 {   
  System.out.println("0 parameter");
 } 

 void createBulb(int a)
 {   
   this.createBulb();
  System.out.println("1 parameter");
 } 
}
class Test159
{  
 public static void main(String args[]){  
  Bulb b1=new Bulb();
  Bulb b2=new Bulb();
  b1.createBulb(100); 
  b2.createBulb(200);  
 }  
}  



















Inside every constructor this() (i.e. call to this) must be first statement. We can use this() inside the constructor only i.e. if we use this() inside method, we will get compile time error.

class Bulb{  
 int watt;  
 String color;
 float price; 
 
Bulb(int w)
 {   
  watt=w;  
 }  
 void createBulb()
 {   
  this();  // call to this inside method is not valid
 }  
} 
class Test190{  
 public static void main(String args[]){  
  Bulb b1=new Bulb(100); 
  b1.createBulb();  
 }  
}  
//O/P-CTE- call to this must be first statement in constructor.
class Employee
{
 int id;
 String name;
 
 void insert(int id,String name,Employee e) 
 {
    e.id=id;
    e.name=name;	
 }
 void show()
 {
  System.out.println(id);
  System.out.println(name); 
 }
 public static void main(String args[])
 {
  Employee e1=new Employee();
  e1.insert(100,"robert",e1); 
  e1.show();
 }
}
  
 
 
 
  
  
class Employee
{
 int id;
 String name;
 
 void insert(int id,String name,Employee e) 
 {
    e.id=id;
    e.name=name;	
 }
 void show()
 {
  System.out.println(id);
  System.out.println(name); 
  System.out.println("this:"+this);
 }
 public static void main(String args[])
 {
  Employee e1=new Employee();
  e1.insert(100,"Robert",e1); 
  e1.show();
  System.out.println("e1:"+e1);
  Employee e2=new Employee();
  e2.insert(102,"David",e2); 
  e2.show();
  System.out.println("e2:"+e2);
 }
}

Footer with Map

Top