This Keyword in Java

HOME

 

This keyword refers to current object in a method or constructor. The most common use of this keyword is to eliminate the confusion between class variables and parameters of same name.

this can also be used to:

  • Invoke current class instance variable
  • Invoke current class method
  • Invoke current class constructor
  • Pass as an argument in the method call
  • Pass as an argument in the constructor call
  • Return the current class object

Invoke current class instance variable

Here, this keywords can be used to refer or invoke the current class instance variable. Suppose the class variables and parameters share the same name, then this ambiguity can be resolved by the use of this keyword.

public class Employee {
     String name;
     String EmpId;
     Employee(String name, String EmpId) {
           name = name;
           EmpId = EmpId;
      }
      void display() {
            System.out.println(name + " " + EmpId);
      }
      public static void main(String[] args) {
            Employee E1 = new Employee("Vibha", "IT0047");
            Employee E2 = new Employee("Abha", "IT0045");
            E1.display();
            E2.display();
     }
}

Output
null null 
null null

The solution to the problem where both class instance variables and parameters have same name can be resolved with the help of this keyword as shown below.

public class Employee {
     String name;
     String EmpId;
 
     Employee(String name, String EmpId) {
         this.name = name;
         this.EmpId = EmpId;
     }
 
      void display() {
          System.out.println(name + " " + EmpId);
      }
 
       public static void main(String[] args) {
             Employee E1 = new Employee("Vibha", "IT0047");
             Employee E2 = new Employee("Abha", "IT0045");
             E1.display();
             E2.display();
    }
}

Output
Vibha IT0047
Abha IT0045

Invoke current class method

The current class method invoked by using this keyword. Here, both display1() and this.display1() means the same.

public class ThisInvokeCurrentClassMethod {
 
    void display1() {
         System.out.println("This is display 1 method");
     }
 
     void display2() {
           System.out.println("This is display 2 method");
           display1();
           this.display1();
     }
 
     public static void main(String[] args) {
           ThisInvokeCurrentClassMethod obj1 = new ThisInvokeCurrentClassMethod();
           obj1.display2(); 
     }
}
 
Output
This is display 2 method
This is display 1 method
This is display 1 method

Invoke current class constructor

The current class constructor can be invoked by using this keyword. This is used to reuse the constructor.

public class ThisInvokeCurrentConstructor {
     ThisInvokeCurrentConstructor() {
           System.out.println("This is display method");
     }
 
     ThisInvokeCurrentConstructor(int x) {
         this();
         System.out.println("Value of X :" + x);
     }
 
     public static void main(String[] args) {
        ThisInvokeCurrentConstructor obj = new ThisInvokeCurrentConstructor(12);
    }
}
 
Output
This is display method
Value of X :12

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s