this keyword in java

There can be a lot of usage of this keyword. In java, this is a reference variable that refers to the current object.

Usage of this keyword

the 6 usage of java this keyword.
  • this can be used to refer current class instance variable.
  • this can be used to invoke current class method (implicitly)
  • this() can be used to invoke current class constructor.
  • this can be passed as an argument in the method call.
  • this can be passed as argument in the constructor call.
  • this can be used to return the current class instance from the method.
  • this: to refer current class instance variable

    The this keyword can be used to refer current class instance variable. If there is ambiguity between the instance variables and parameters, this keyword resolves the problem of ambiguity.

    problem without using this keyword

    Example:

    class Person{  
    int rollno;  
    String name;  
    float fee;  
    Person(int rollno,String name,float fee){  
    rollno=rollno;  
    name=name;  
    fee=fee;  
    }  
    void display(){System.out.println(rollno+" "+name+" "+fee);}  
    }  
    class TestThis1{  
    public static void main(String args[]){  
    Person p1=new Person(101,"Ramesh",8000f);  
    Person p2=new Person(102,"Sumun",9000f);  
    p1.display();  
    p2.display();  
    }
    }  
    

    Output:

    0 null 0.0
    0 null 0.0
    
    in this example, parameters (formal arguments) and instance variables are same. So, we are using this keyword to distinguish local variable and instance variable.

    Solution of the above problem by this keyword

    class Person{  
    int rollno;  
    String name;  
    float fee;  
    Person(int rollno,String name,float fee){  
    This.rollno=rollno;  
    This.name=name;  
    This.fee=fee;  
    }  
    void display(){System.out.println(rollno+" "+name+" "+fee);}  
    }  
    class TestThis1{  
    public static void main(String args[]){  
    Person p1=new Person(101,"Ramesh",8000f);  
    Person p2=new Person(102,"Sumun",9000f);  
    p1.display();  
    p2.display();  
    }
    }  
    

    Output:

    101   Ramesh  8000
    102   Suman   9000
    

    this: to invoke current class method

    You may invoke the method of the current class by using the this keyword. If you don't use the this keyword, compiler automatically adds this keyword while invoking the method.

    Example:

    class P{  
    void q() {
    System.out.println("Hello q java");}  
    void r() {  
    System.out.println("Hello r java");  
          //q();//same as this.q()  
    this.q();  
    }  
    }  
    class TestThis2 {  
    public static void main(String args[])  {  
    P p = new P();  
    p.r();  
    }
    }
    

    Output:

    Hello  q   java
    Hello  r   java
    

    this() : to invoke current class constructor

    The this() constructor call can be used to invoke the current class constructor. It is used to reuse the constructor. In other words, it is used for constructor chaining.

    Example:

    class P {  
    P() {
    System.out.println("Hello Catchmecoder");}  
    P(int y) {  
    this();  
    System.out.println(y);  
    }  
    }  
    class TestThis3 {  
    public static void main(String args[]) {  
    P p = new P(15);  
     }
     }
    

    Output:

    Hello Catchmecoder
    15
    

    this: to pass as an argument in the method

    The this keyword can also be passed as an argument in the method. It is mainly used in the event handling

    Example:

    class P2 {  
    void r(P2 obj) {  
    System.out.println("passed an argument in method");  
      }  
    void s() {  
    r(this);  
     }  
    public static void main(String args[])  {  
    P2 p1 = new P2();  
    p1.s();  
      }  
      } 
    

    Output:

    passed an argument in method

    this: to pass as argument in the constructor call

    We can pass the this keyword in the constructor also. It is useful if we have to use one object in multiple classes.

    Example:

    class P  {  
    R3 obj;  
    P(R3 obj) {  
    this.obj=obj;  
      }  
    void display() {  
    System.out.println(obj.data); //using data member of R3 class  
      }  
      }  
      
    class R3 {  
    int data=15;  
    R3() {  
    P p = new P(this);  
    p.display();  
      }  
    public static void main(String args[]){  
    R3 r = new R3();  
      }  
     }    
    

    Output:

    10

    this keyword can be used to return current class instance

    We can return this keyword as an statement from the method. In such case, return type of the method must be the class type (non-primitive).

    Syntax:

    return_type method_name(){  
    return this;  
     }
    

    Example:

    class P  {  
    P getP()  {  
    return this;  
      }  
    void msg() { 
    System.out.println("Hello Catchmecoder");
       }  
       }  
    class Test5 {  
    public static void main(String args[])  {  
    new P().getP().msg();  
      }  
      }  
    

    Output:

    Hello Catchmecoder