Keywords are specific terms that serve as a key to a code. Java has its own reserved predefined keywords such as do, for, if, and much more. These keywords have a specific meaning defined in the language library. In the Java program, the “this” keyword acts as a reference variable pointing toward the current object.
This blog will discuss the usage of the Java keyword “this” with examples.
What is “this” Keyword in Java?
The Java keyword “this” belongs to a reference variable, pointing to the current object of the program. The main use of the “this” keyword is to distinguish a class attribute from a parameter. It can also be utilized to get any member or method of the current object.
Uses of “this” Keyword in Java
Here, we have listed several uses of the Java “this” keyword:
- “this” keyword is utilized for referring to and accessing the class instance variable.
- The method and the constructor call also use it as an argument.
- It is used to call the method of the same class.
Now, let’s check out some practical examples of using the “this” keyword in Java.
How to Use Java “this” Keyword to Refer to Current Instance?
This example will teach the procedure of using the “this” keyword for referring to and accessing the current instance variable. For this purpose, firstly, we will create two instance variables:
int vTwo;
In our class constructor, we will pass two parameters with the same name as instance variables. The class variables and the method’s parameters can have the same name, and the “this” keyword will be used to eliminate the ambiguity that results from this:
this.vOne = vOne + vOne;
this.vTwo = vTwo + vTwo;
}
Now, we will create a method named Print() to print out the values of the current variable instances:
In the main() method, we will create an object of our Java class and pass the values as arguments to its constructor. After doing so, call the Print() method by using the created class object:
The given output indicates that the “this” keyword has successfully accessed and displayed the values of current variables instances:
Now, let’s head towards the next section.
How to Use Java “this” Keyword to Invoke Current Class Method?
This section will demonstrate the procedure of using the “this” keyword for calling the current class method. To do so, first, we will create a method named display() in which we will declare two integer type variables vOne, and vTwo, and initialize them with the given values. After doing so, we will create a new variable named value that comprises the sum of the existing variables:
int vOne=2;
int vTwo=5;
int value= vOne + vTwo;
System.out.println("The value is:" + value);
}
Create one more method named Print() that will invoke the display() method in its function definition by using “this” keyword:
this.display();
}
In the main() method, we will create an object named obj of the JavaKeyword class and call the Print() method with it:
As you can see, the “this” keyword successfully called the specified method of our class and displayed the resultant value:
We have provided the essential information related to using the “this” keyword in a Java program.
Conclusion
In Java, the keyword “this” belongs to a reference variable and points to the current object of the Java program. The main use of the “this” keyword is to distinguish a class attribute from a parameter. It can be utilized to get any member or method of the current object. This blog discussed the Java “this” keyword and its uses with examples.