How to use this keyword in java with detail example

There are many ways of how to use this keyword in Java. In Java, "this" is a reference variable that references the current object.

The use of this keyword in Java

In Java, this keyword has six uses, as follows:
  • This keyword can be used to reference an instance variable of a class.
  • This () can be used to summon the Constructor of the current class.
  • This keyword can be used to summarily overlap the current class method.
  • This keyword can be passed as a parameter in the method call.
  • This keyword can be passed as a parameter in the Constructor call.
  • This keyword can also be used to return an instance of the current class.
Recommend: If you are new to Java, you should only track two ways of use of "this" keyword.

This keyword can be used to reference an instance variable of the current class

If there is ambiguity between the instance variable and the parameter, the this keyword treats the problem. First, let's look at an example to understand what happens when you do not use the this keyword.
Example:
this-keyword-in-java
In this example, the parameters and instance variables are the same, and that's why we use the this keyword to distinguish between local variables and instance variables.
Handle the above problem by this keyword in Java
this-keyword-in-java
If local variables and instance variables are different, it is not necessary to use the this keyword as in the following Example:
this-keyword-in-java
This () can be used to summon the constructor of the current class
The call to this () constructor can be used to summon the Constructor of the current class. This approach is better if you have multiple constructors in the class and want to reuse that constructor.
this-keyword-in-java
Question: Where to use this () constructor?
The call to this () constructor should be used to reuse the constructor. It maintains the chain between the constructor, and is used for the Chaining Constructor. You follow this example to understand more about this.
this-keyword-in-java
Rule: The call to this () must be the first command in the constructor.
In the following example program will give an error message at compile time.
this-keyword-in-java
This keyword can be passed as a parameter in the method
This method is mainly used in event handlers. You follow the example:
this-keyword-in-java
This keyword can be passed as a parameter in the constructor.
You can also pass the this keyword in the constructor. It is useful if we must use an object in multiple classes. You follow the example:
this-keyword-in-java
"This " keyword can be used to return an instance of the current class
We can return the keyword this as a command from the method. In this situation, the return type of the method must be of type class (not the original type). You follow the example:
The syntax of "this" that can be returned as a command.
Example:
this-keyword-in-java
We also prove that the keyword "this" refers to the instance variable of the current class. In the following program, we print the reference variable and "this", the result of which is the same.
this-keyword-in-java

Comments