This keyword in java - Use and its Reference !



There are many keywords in java language that are reserved and can't be used without caution. One such keyword is "this".

This is a keyword in java that has specific meanings in specific scenarios.
This refers to the object itself.

Some of the main scenarios in which we use "this" are as follows:
 

1. To refer to the instance variable of the class in which "this" keyword is used

This means that in the current class there are instance variables, variables such as these are created in a class as parameters and generally used to store the data provided as arguments to the constructor.

Using the "this" keyword we can clearly show which variables we are talking about as it is difficult for the compiler. to make this distinction on its own.

An example of this would be as follows.
 public class example {
      private int x;
      public void setX(int x) {
      x=x;
      }
}

public class example {
    private int x;
    public void setX(int x) {
    this.x=x;
    }
}
In the above two examples, one has the "this keyword" and the other doesn't.

What is the difference between the two?

well, the difference is "this.x" instead of just x.

What does "this.x" change?

It provides an understanding to the compiler that the "this.x" is the instance variable of the class and the values passed as arguments are to be stored in it.

This distinction would be difficult for the computer to make on its own as "x=x" doesn't provide the necessary distinction as to which variable is on the left i.e. the one that stores the value & which is on the right i.e. the one that is the value to be stored(argument).


2. Invoking an overloaded current class constructor using "this" keyword

In java, a constructor is like a function with the same name as the class itself.

Sometimes when different inputs require constructors with different parameters, it becomes difficult without the "this" keyword.

For example, if you have two different constructors in a class in which either one or two arguments are passed from the main function and depending on it you have to pass arguments to the respective parametrized constructor.

But what if you want that if only 1 input is given?
That is what if you wish to pass it to the second constructor with a default value? 

Here in this case "this" keyword comes into play.

Here is the code for the above scenario,

class thiskeywordtesting{
    
    int value1;
    int value2;
  
    thiskeywordtesting(){

        this(100,1000);
        System.out.println("This is the default constructor that doesn't take any parameters");
    }
  
    thiskeywordtesting(int value1, int value2){

        this.value1 = value1;
        this.value2 = value2;

        System.out.println("This is the parameterized constructor which is called using 'this'");
    }   
}
        
public class Main{
 
    public static void main(String[] args){

        thiskeywordtesting test = new thiskeywordtesting();
    }   
}


3. Invoke current class methods

Using the "this" keyword inside a method allows you to call another method from the same class. 


4. Return the current class instance

"return this;" inside a constructor is the code we are talking about in this scenario.

By returning "this" we are returning the current class instance variable details to the function/method that calls this constructor. 


These are ways of handling code using the "this" keyword that are great for maintaining a set logic and removing unnecessary code. 

Post a Comment

Previous Post Next Post