Java this keyword

Java this keyword refers to the current instance of the class. Every constructor and all non-static methods in Java have this as an implicit parameter.

Note: When n numbers of parameters are passed in a method then from the JRE point of view n+1 number of parameters are passed in the method. One additional parameter is this. If this keyword is used in constructor or method then this must be the first statement. 

Use of this keyword in Java:

  1. this can be used to differentiate between instance variables and local variables.
  2. this can be used in constructor chaining. this() and super() must be the first statement.
  3. this can be used to invoke the current class method implicitly.
  4. this can be used to return the current instance of the class.
  5. this can be used as a parameter in the constructor call.
  6. this can be used as a parameter in the method call.

1. this can be used to differentiate between instance variable and local variable.

If the local variables and instance variables are the same then the compiler will not be able to distinguish them.

class Student{
      //instance variable.
      int rollNo;
      String name;              

      Student(String name, int rollNo){
           //local variable.
           name = name;
           rollNo = rollNo;
      }               

      public void displayDetails(){
             System.out.println("RollNo = " + rollNo);
             System.out.println("Name = " + name);
      }
}

public class ThisExample1 {
       public static void main(String args[]){
              //creating Student class object.
              Student stu1 = new Student("Roy", 11);
              //method call
              stu1.displayDetails();
      }
}

Output

RollNo = 0 
Name = null

If the local variables and instance variables are the same then the compiler will not be able to distinguish them. this can be used to differentiate between instance variable and local variable

class Student{
      //instance variable.
      int rollNo;
      String name;               

      Student(String name, int rollNo){
             //local variable.
             this.name = name;
             this.rollNo = rollNo;
      }               

      public void displayDetails(){
             System.out.println("RollNo = " + rollNo);
             System.out.println("Name = " + name);
      }
}

public class ThisExample2 {
       public static void main(String args[]){
              //creating Student class object.
              Student stu1 = new Student("Roy", 11);
              //method call
              stu1.displayDetails();
       }
}

Output

RollNo = 11 
Name = Roy

If the local variables and instance variables are different then no need for this keyword.

class Student{
      //instance variable.
      int rollNo;
      String name;               

      Student(String n, int r){
             //local variable.
             name = n;
             rollNo = r;
      }

      public void displayDetails(){
              System.out.println("RollNo = " + rollNo);
              System.out.println("Name = " + name);
      }
}

public class ThisExample3 {
       public static void main(String args[]){
              //creating Student class object.
              Student stu1 = new Student("Roy", 11);
                                
              //method call
              stu1.displayDetails();
       }
}

Output

RollNo = 11 
Name = Roy

2. this can be used in constructor chaining.

Constructor chaining is a process of calling one constructor from another constructor of a class on an object.

class Display {
      int a, b;
                
      //default constructor.
      Display(){
          //call two parameter constructor using this.
          this(10, 20);
           System.out.println("Default Constructor called.");
     }             

     //one parameter constructor.
     Display(int num1){
             //call two parameter constructor using this.
             this(num1, 40);
             System.out.println("one parameter constructor called.");
     }              

     //two parameter constructor.
     Display(int num1, int num2){
            a = num1;
            b = num2;
            System.out.println("two parameter constructor called.");
     }              

     //method to display values.
     public void display(){
            System.out.println("a = " +a);
            System.out.println("b = " +b);
     }
}

public class ThisExample7 {
        public static void main(String args[]){
               //call default constructor.
               Display obj1 = new Display();
               obj1.display();

              //call one parameter constructor.
              Display obj2 = new Display(30);
              obj2.display();

              //call two parameter constructor.
              Display obj3 = new Display(50, 60);
              obj3.display();
       }
}

Output

two parameter constructor called.
Default Constructor called.
a = 10
b = 20
two parameter constructor called.
one parameter constructor called.
a = 30
b = 40
two parameter constructor called.
a = 50
b = 60

3. this can be used to invoke the current class method implicitly.

Method chaining is a process of calling multiple methods on an object in a single statement.

class Display{
      public void displayName(){
             System.out.println("Roy");

             //call current class method using this
             this.displayRollNo();

             }

             public void displayRollNo(){
                    System.out.println("11");

                    //compiler will automatically add this
                    //keyword if not used.
                    displayClass();
                }

                public void displayClass(){
                       System.out.println("MCA");
                }
}

public class ThisExample4 {
       public static void main(String args[]){
              //create Display class object
              Display display = new Display();

             //method call
             display.displayName();
       }    
}

Output

Roy
11
MCA

4. this can be used to return the current instance of the class.

class Display{
      public Display getDisplay(){
             //this return the current class object.
             return this;
      }
      
      public void display(){
             System.out.println("Hello W3schools360.com");
      }
}

public class ThisExample6 {
       public static void main(String args[]){
              //create Display class object
              Display display = new Display();

              //method call, here getDisplay() returns the
              //object of current Display class.
              display.getDisplay().display();
       }  
}

Output

Hello W3schools360.com

5. this can be used as a parameter in the constructor call.

class Display{
      Display(Show obj){
             System.out.println("Show obj = " + obj);
      }
}

class Show{
      Show(){
           //pass show class object as an argument using this.
           Display obj = new Display(this);
      }
}

public class ThisExample8 {
       public static void main(String args[]){
               //create Show class object
               Show show = new Show();
       }     

Output

Show obj = com.w3schools.Show@15db9742

6. this can be used as a parameter in the method call.

class Display{
      public void displayName(){
             System.out.println("W3schools360.com");

             //passing this keyword as an argument.
             displayObject(this);
     }

     public void displayObject(Display obj){
            //will print string representation of the object.
            System.out.println(obj);
     }
}

public class ThisExample5 {
        public static void main(String args[]){
                //create Display class object
                Display display = new Display();

                //method call
                display.displayName();
       }     
}

Output

W3schools360.com
com.w3schools.Display@15db9742

 

Note: this is a final variable, so you can’t assign any value to this. this can’t be used in the static method.