This Keyword in Java

Table of Contents

This Keyword in Java

Usage of this keyword has a huge scope of usability in Java. The main usage of this keyword is supposed to be the usage of referring to the current class instance variable.

Ok, but before we jump into the usages of this keyword, let’s make it clear that is it referring to the same current class instance variable or not, using an example.

class Sample
 {  
 void a()
 {  
  System.out.println(this);// will print the same reference ID  
 }  
  public static void main(String args[])
  {   
  Sample obj=new Sample();  
  System.out.println(obj);//prints a reference ID  
  obj.a();  
 }  
}  

Here you will see two results, which will be Reference ID of this keyword where it references and the reference ID of the object created.

Here are some listing of important usage of this keyword:

  • to refer to the current class instance variable.
  • to invoke current class method (implicitly)
  • to invoke the current class constructor.
  • it can be passed as an argument in the method call.
  • it can be passed as an argument in the constructor call.
  • it can be used to return the current class instance from the method.

Now ,let’s understand each of the usage with some proper examples:

To refer current class instance variable

Sometimes it is important for the programmers to keep multiple names the same but to maintain the readability is tough. So, here comes the usage of this which gives the programmer to use the same name multiple times.

class Simple
{  
   int a;  
   Student(int a)
   {  
   this.a = a; 
   }  
void display(){System.out.println(a);}  
}  
class Sample
{  
   public static void main(String args[])
   {  
   Simple b=new Simple(5);   
   b.display();  
   }
}  

Here, you can notice the function of this keyword. It is used to refer to the current class instance variable here.

Thus we can conclude that if there is ambiguity between the instance variables and parameters, this keyword resolves the problem of ambiguity.

To invoke the current class method

class Simple
{  
  void b(){System.out.println("B");}  
  void c()
  {  
  System.out.println("C");  
  this.b();  //here this keyword used,will help to invoke current class method 
}  
}  
class Sample
{  
  public static void main(String args[])
  {  
   Simple a=new Simple();  
   a.c();  
  }
} 

To invoke the current class constructor

So, as we know the constructors are of two types, Parametrized and non-Parameterized(Default) Constructor, here we will look into the Parameterized one only.

class Simple
 {  
  Simple()
  {  
   this(5);  
   System.out.println("Second");  
  }  
  Simple(int x)
  {  
   System.out.println("First");  
   }  
}  
class Sample
{  
  public static void main(String args[])
  {  
  Simple a=new Simple();  
  }
}  

To pass as an argument in the method

class Sample
{  
  void second(Sample obj)
  {  
   System.out.println("argument passed successfully");  
  }  
 void first()
 {  
  second(this);  
 }  
  public static void main(String args[])
  {  
    Sample a = new Sample();  
    a.first();  
  }  
}

To pass as argument in the constructor call

class Simple
{  
  Sample obj;  
  Simple(Sample obj)
    {  
      this.obj=obj;  
     }  
  void display()
    {  
      System.out.println(obj.a);
    }  
}  
  
class Sample
{  
  int a = 5;  
  Sample()
  {  
   Simple b=new Simple(this);  
   b.display();  
  }  
  public static void main(String args[])
  {  
   Sample a=new Sample();  
  }  
} 

To be used to return current class instance

class Simple
 {  
   Simple soup()
   {  
    return this;  
   }  
  void msg(){System.out.println("Returned as a statement");}  
 }  
class Sample
 {  
  public static void main(String args[])
  {  
   new Simple().soup().msg();  
   }  
 } 

Leave a Reply