THIS KEYWORD IN JAVA

This keyword in Java is a reference variable that is used to refer to the current class object inside a method or a constructor. It is mainly used to eliminate the confusion between the class attributes and parameters with the same name.

Table of Contents

[hide]
  • What is this keyword in Java
  • Refer Current Class Instance
  • Invoke Current Class Method
  • Invoke Current Class Constructor
  • Used as a Method Parameter
  • Return Current Class Instance

What is this Keyword in Java

This keyword in Java is used for various purposes. The most common use of this keyword is to differentiate variable of a class and formal parameters of methods and constructors.

Whenever the formal parameter and data member of a class are similar and the JVM gets an amiguity, in that case we should use this keyword.

Syntax : this keyword

this. this () 

1. this. is used to differentiate the variableof a class with the formal parameters of methods and constructors while pointing to the current class object. It can be used as :

this.instance variable of current class.

2. this( ) is used to call one constructor within another without creating multiple object of a same class.

this(); //call default constructor
this(param1, param2, ....); //call parametrized constructor

Some other usage of Java this keyword are :

  • this Keyword is used to refer instance variables of the current class.

  • this keyword is used to Invoke current class method.

  • this keyword is used to Invoke current class constructor.

  • this keyword can be used as a method parameter.

  • this keyword can be used to Return Current Class Instance

Lets learn more about these usage of this keyword with examples.

Refer Current Class Instance

Java does not allow to declare a instance variable with the same name again, but it allows instance variable and parameters to have the same name. In this case there is an amiguity, the compiler gets confused between the instance variable and parameter.

Let's take an example where we have instance variables and parameters with the same name and what problem occurs when we use them.

Example : this keyword used to refer current class instance variable

copyimage
//Java Program to demonstrate the use of this keyword in Java
class Department { 
    String name;
    int count;

  public void setinfo(String name, int count) {
    name=name;
    count=count;
  }

  public void showdata() {
    System.out.println("Department name : "+ name);
    System.out.println("No of Student : "+ count);
  }
} 
public class Codebator { 
 public static void main (String[] args) { 
    Department s1 = new Department();
    s1.setinfo("Computer Science", 500);
    s1.showdata();
  } 
} 

OUTPUT :

Department name : nullNo of Student : 0 

Now you must be thinking intead of getting the correct values, the output generated is null and 0. During the execution compiler gets confused whether the instance on the left hand side of an operator is an instance variable or a global variable

The solution is "THIS" keyword, it differentiates instance variable from the local variable. Let us see the same program with the solution.

copyimage
//Java Program to demonstrate the use of this keyword in Java
class Department { 
    String name;
    int count;

  public void setinfo(String name, int count) {
    this.name=name;
    this.count=count;
  }

  public void showdata() {
    System.out.println("Department name : "+ name);
    System.out.println("No of Student : "+ count);
  }
} 
public class Codebator { 
 public static void main (String[] args) { 
    Department s1 = new Department();
    s1.setinfo("Computer Science", 500);
    s1.showdata();
  } 
} 

OUTPUT :

Department name : Computer ScienceNo of Student : 500 

In real time projects its good to use meaningful names for variables, so its recommended to use same name for instance variable and parameter.

Invoke Current Class Method

Current Class Method can be invoked by using this keyword, if you do not use this keyword, the compiler will automatically add this keyword when invoking the method.

Example : this keyword used to refer current class method

copyimage
//Java Program to demonstrate the use of this keyword in Java
class Department { 
     String name;
     int count;

   public void setinfo(String name, int count) {
     this.name=name;
     this.count=count;
     this.showdata(); 
   }

   public void showdata() {
     System.out.println("Department name : "+ name);
     System.out.println("No of Student : "+ count);
   } 
} 
public class Codebator { 
   public static void main (String[] args) { 
      Department s1 = new Department();
      s1.setinfo("Computer Science", 500);
   } 
} 

OUTPUT :

Department name : Computer ScienceNo of Student : 500 

Invoke Current Class Constructor

Current Class Constructor can be invoked by using this keyword, we can either call a default constructor from a parameterized constructor or vice versa. Lets see an example of calling a parameterized constructor from default constructor.

Example : calling a parameterized constructor from default constructor.

copyimage
//Java Program to demonstrate the use of this keyword in Java
class Department { 
     String name;
     int count;

    // default constructor
    Department(){
    this("Computer Science", 500);
    System.out.println("Inside Default Constructor ");
    }
   
    //Parameterized  constructor
    Department(String name, int count){
    this.name=name;
    this.count=count;
    this.showdata();
    System.out.println("Inside Parameterized Constructor ");
    }

    public void showdata() {
    System.out.println("Department name : "+ name);
    System.out.println("No of Student : "+ count);
   } 
} 

public class Codebator { 
    public static void main (String[] args) { 
       Department s1 = new Department();  
    } 
} 

OUTPUT :

Department name : Computer ScienceNo of Student : 500 Inside Parameterized Constructor Inside Default Constructor 

Constructor Chaining

this keyword is also used for constructor Chaining i.e reuse constructor from another constructor. Lets see an example how you can resuse the constructor.

Example : Constructor Chaining

copyimage
//Java Program to demonstrate the use of this keyword in Java
class Department { 
   String name, tech;
   int count;

  Department(String name, int count){
  this.name = name;
  this.count = count;
  }
  
  Department(String name, int count, String tech){
  this(name,count); // reusing constructor
  this.tech = tech;
  }

  public void showdata() {
  System.out.println(name +" "+count +" "+tech);
 } 
} 

public class Codebator { 
  public static void main (String[] args) { 
     Department s1 = new Department("Computer Science",500); 
     Department s2 = new Department("Computer Science",500, "Java");
     s1.showdata();
     s2.showdata();
  } 
} 

OUTPUT :

Computer Science 500 nullComputer Science 500 Java 

Remember that Call to this() must always be the first statement in constructor else it will through you an error.

Used as a Method Parameter

this keyword can also be passed as a method parameter.

Example : Using ‘this’ keyword as method parameter

copyimage
//Java Program to demonstrate the use of this keyword in Java
class Department { 
   String name;
   int count;

  // default constructor
  Department(){
  name="Computer Scinece";
  count=500;
  }

  // Method that receives 'this' keyword as parameter 
  void display(Department obj) 
  {   System.out.println("Department name :"+name); 
      System.out.println("No of Student :"+count); } 

  // Method that returns current class instance 
  void get() 
  {  display(this); } 
} 

public class Codebator { 
  public static void main (String[] args) { 
     Department s1 = new Department();  
     s1.get();
  } 
} 

OUTPUT :

Department name :Computer ScineceNo of Student :500 

Return Current Class Instance

this keyword is also used to return the current class instance. Let's see this with the help of example.

Example : Using ‘this’ keyword to return the current class instance

copyimage
//Java Program to demonstrate the use of this keyword in Java
class Department { 
    String name;
    int count;

   //Default constructor 
   Department() 
   { name="Computer Scinece";
     count=500; } 
   
   //Method that returns current class instance 
   Department get() 
   {  return this; } 

 //Displaying value of variables name and count 
   void display() 
   {  System.out.println("Department name :"+name); 
      System.out.println("No of Student :"+count); 
   } 
} 

public class Codebator { 
  public static void main (String[] args) { 
     Department s1 = new Department();  
     s1.get().display();
  } 
} 

OUTPUT :

Department name :Computer ScineceNo of Student :500 

this keyword might be confusing for begineers, kindly learn only few important usages of this keyword.