«Back to Home

Core Java

Topics

This Keyword In Java

This Keyword

In Java, “this” keyword is a special keyword, which is used as a reference variable, which refers to the current class object. “this” keyword is widely used in Java.
 
Usage of this keyword in Java
  1.  “this” keyword is used to refer a current class instance variable.

    “this” keyword solves the problem of ambiguity between the instance variable and a parameter.

    Let’s see an example, given below.

    Code
    1. public class Car {  
    2.     int cnum;  
    3.     String cname;  
    4.     String color;  
    5.   
    6.     Car(int cnum, String cname, String color) {  
    7.   
    8.         this.cnum = cnum;  
    9.         this.cname = cname;  
    10.         this.color = color;  
    11.     }  
    12.   
    13.     void carInfo() {  
    14.         System.out.println(cnum + " " + cname + " " + color);  
    15.     }  
    16.   
    17.     public static void main(String args[]) {  
    18.         Car c1 = new Car(10235"Audi""Red");  
    19.         Car c2 = new Car(23068"Swift""White");  
    20.         c1.carInfo();  
    21.         c2.carInfo();  
    22.     }  
    23. }  
    g

    Output

    h

    In the example, shown above, parameter and instance variables are same due to which we need to use this keyword to distinguish between local variable and instance variable. If we are not used this keyword in this program. Thus, it creates a problem of ambiguity.

    If the local variable and instance variables are different, “this” keyword is not required.

    Let’s see an example, given below.

    Code
    1. public class Car {  
    2.     int cnum;  
    3.     String cname;  
    4.     String color;  
    5.   
    6.     Car(int n, String cn, String c) {  
    7.         this.cnum = n;  
    8.         this.cname = cn;  
    9.         this.color = c;  
    10.     }  
    11.     void carInfo() {  
    12.         System.out.println(cnum + " " + cname + " " + color);  
    13.     }  
    14.     public static void main(String args[]) {  
    15.         Car c1 = new Car(10235"Audi""Red");  
    16.         Car c2 = new Car(23068"Swift""White");  
    17.         c1.carInfo();  
    18.         c2.carInfo();  
    19.     }  
    20. }  
    i

    Output
    j

    In the example, shown above, there is no need to use this keyword since local variables and instance variables are different in this program.

  2. this keyword is used to call current class constructor.

    “This” keyword is used to call the current class constructor. It is used mainly when we use many constructors in the class and its main advantage is to reuse the constructor.

    Let’s see an example, given below.

    Code
    1. public class Car {  
    2.     int cnum;  
    3.     String cname;  
    4.     String color;  
    5.   
    6.     Car() {  
    7.         System.out.println("default constructor");  
    8.     }  
    9.   
    10.     Car(int cnum, String cname, String color) {  
    11.         this(); //it is used to call current class constructor.    
    12.         this.cnum = cnum;  
    13.         this.cname = cname;  
    14.         this.color = color;  
    15.     }  
    16.   
    17.     void carInfo() {  
    18.         System.out.println(cnum + " " + cname + " " + color);  
    19.     }  
    20.   
    21.     public static void main(String args[]) {  
    22.         Car c1 = new Car(10235"Audi""Red");  
    23.         Car c2 = new Car(23068"Swift""White");  
    24.         c1.carInfo();  
    25.         c2.carInfo();  
    26.     }  
    27. }  
    k

    Output

    l

    In the example, shown above, the current class constructor invokes first, followed by other because we use this keyword but this () must be the first statement in the constructor.

  3. this keyword is used to call current class method.

    “this” keyword is also used to call the method of the current class. If we don’t use “this” keyword, by default, the compiler automatically adds this keyword.

    Let’s see an example, given below.

    Code
    1. public class Car1 {  
    2.   
    3.     void run() {  
    4.         System.out.println("method is call");  
    5.     }  
    6.   
    7.     void slow() {  
    8.         this.run(); //no need because compiler does it for you.    
    9.     }  
    10.   
    11.     void fast() {  
    12.         slow(); //complier will add this to call slow() method as this.slow()    
    13.     }  
    14.   
    15.     public static void main(String args[]) {  
    16.         Car1 s1 = new Car1();  
    17.         s1.fast();  
    18.     }  
    19. }  
    m

    Output
    n

  4. this keyword passed as an argument in the method call.

    “this” keyword is passed as an argument in the method call and it is used in the event handling.

    Let’s see an example, given below.

    Code
    1. public class Car2 {  
    2.   
    3.     void run(Car2 obj) {  
    4.         System.out.println("method call");  
    5.     }  
    6.   
    7.     void fast() {  
    8.         run(this);  
    9.     }  
    10.   
    11.     public static void main(String args[]) {  
    12.         Car2 c1 = new Car2();  
    13.         c1.fast();  
    14.     }  
    15. }  
    p

    Output

    q

  5. this keyword is passed as an argument in the constructor call.

    “this” keyword passed can be called in the constructor as an argument also. It is mainly used, if we want to use one object in the multiple classes.

    Let’s see an example, given below.

    Code
    1. class Car3 {  
    2.   
    3.     Bike3 obj;  
    4.   
    5.     Car3(Bike3 obj) {  
    6.         this.obj = obj;  
    7.     }  
    8.   
    9.     void displayInfo() {  
    10.         System.out.println(obj.speed); //using data member of A4 class    
    11.     }  
    12. }  
    13.   
    14. public class Bike3 {  
    15.   
    16.     int speed = 60;  
    17.   
    18.     Bike3() {  
    19.         Car3 c = new Car3(this);  
    20.         c.displayInfo();  
    21.     }  
    22.   
    23.     public static void main(String args[]) {  
    24.         Bike3 b = new Bike3();  
    25.     }  
    26. }  
    o

    Output

    r

  6. this keyword can be used to return the current class instance.

    “this” keyword is also used to return the current class instance. In it, return type should be the class type.

    Let’s see an example, given below.

    Code
    1. class Bike4 {  
    2.   
    3.     Bike4 getBike4() {  
    4.         return this;  
    5.     }  
    6.   
    7.     void run() {  
    8.         System.out.println("run fastly");  
    9.     }  
    10. }  
    11.   
    12. public class Car4 {  
    13.   
    14.     public static void main(String args[]) {  
    15.         Bike4 b = new Bike4();  
    16.         b.getBike4();  
    17.         b.run();  
    18.     }  
    19. }  
    s

    Output

    t
Summary

Thus, we learnt “this” keyword is a special keyword, which is used as a reference variable, which refers to the current class object and also learnt how to use it in Java.