In this tutorial, we will learn about this keyword in Java. We will explore the details of βthisβ keyword and also present some examples of its usage in Java.
The keyword βthisβ in Java is a reference variable. The reference variable βthisβ points to the current object in the Java program. Hence if you want to access any member or function of the current object, then you can do so by using βthisβ reference.
Letβs see what is this keyword in Java, And how we explain this keyword in Java.
This keyword in Java Java is an object-oriented and class-based language developed by Sun Microsystems. It has a diverse range of keywords that are used to perform predefined or some internal processes in the code. We cannot use these keywords as variables, classes, objects, or any other type of classifiers. The programmer can simply use the keyword and perform the needed action. Β There are approximately 51 keywords in Java language. Some of the most useful ones are abstract, break, case, char, class, continue, float, for, if, null, private, protected, try, this, void, while etc. Out of these 51, 2 keywords are reserved that means for future use: βconstβ and βgotoβ.
One of these keywords in Java is βthisβ keyword. When we initialize an object in the class, this keyword helps to refer to that particular object of the class whose method is being called by the coder. This keyword can be used either inside the class of the program or in the constructor. This() can invoke the constructor of the current class being worked on which is then passed as an argument in the method call or in the constructor call.
This keyword has the following properties:
- This keyword is used to get the current object in the program P.
- The keyword is used to invoke the current method of the object.
- This keyword is used to return the current object from the method.
- This() can be invoked in the current class constructor C.
- This keyword can also be passed as a parameter to the constructor C.
- The keyword can be passed as an argument in the constructor call.
- This keyword eliminates the chance of naming conflicts in a constructor or method for the object.
public class ClassX{ //Initialise the class
int a;
public ClassX(int a) { //Constructor with a parameter
this.a = a;
}
public static void main(String[] args) {
ClassX myObj = new ClassX(5); //Calling the constructor
System.out.println("Value of a = " + myObj.a);
}
}
Output :
Value of a = 5.
Hence, the most useful property of this keyword that we understand is that it gets rid of the confusion between class parameters and attributes that too with the same name. Try rephrasing the code above without using βthisβ keyword. You will get the output as 0.
Example for better understanding of this keyword
public class ClassA { // Declaring the class as public
int x;
int y;
ClassA() { // Default constructor
x = 5;
y = 10;
}
void display() { // Modified the display method to use instance variables directly
System.out.println("x = " + x + " y = " + y);
}
void get() { // Modified the get method to directly call display()
display();
}
public static void main(String[] args) {
ClassA object = new ClassA();
object.get();
}
}
Output :
x = 5 y = 10
Class: ClassA .
Instance variables: x and y.
Method get(): To get the data for x and y.
Return with ‘this’ keyword
If the return type of the method is the object of the current class, then you can conveniently return βthisβ pointer. In other words, you can return the current object from a method using βthisβ pointer.
public class Test_this {
int val_a;
int val_b;
// Default constructor
Test_this() {
val_a = 10;
val_b = 20;
}
public Test_this get() {
return this;
}
void display() {
System.out.println("val_a = " + val_a + " val_b = " + val_b);
}
}
public class Main {
public static void main(String[] args) {
Test_this object = new Test_this();
object.get().display();
}
}
Output :
val_a = 10 val_b= 20
Difference between this and super keyword in Java
As we have discussed in the previous section, βthisβ keyword is used to access the methods of the current class in Java. However βsuperβ keyword can access the methods of the parent class in Java. We can never use this keyword as an identifier as it is one of the reserved keywords in Java.
This keyword can also be used for accessing static members in Java. Whereas, the super keyword is majorly used to access parent class methods as well as parent class constructors. If both these keywords are used together, they will give a compile-time error. Hence, they can never be used together.