In Java programming language, 'this' is a reference variable that refers to the current object. There are different ways to use the 'this' keyword in Java.
Using 'this' keyword to refer to the current class instance variables: This allows you to distinguish between the class instance variable and the local variable with the same name.
Using this() to invoke the current class constructor: This is used to invoke a constructor from another constructor in the same class.
Using 'this' keyword to return the current class instance: This is used to return the current class instance from a method.
Using 'this' keyword as a method parameter: This is used to pass the current class instance as a parameter to a method.
Using 'this' keyword to invoke the current class method: This is used to call another method within the same class.
Using 'this' keyword as an argument in the constructor call: This is used to create an object of one class within another class.
Here is an example of each usage of 'this' keyword in Java with detailed explanation and output:
- Using 'this' keyword to refer to the current class instance variables:
The 'this' keyword is used to refer to the instance variables of the current class. This is useful when the method parameter has the same name as the instance variable. In this example, we have two instance variables, 'a' and 'b', and we use 'this' to refer to them.
csharpclass Test {
int a;
int b;
Test() {
this(10, 20);
System.out.println("Inside default constructor");
}
Test(int a, int b) {
this.a = a;
this.b = b;
System.out.println("Inside parameterized constructor");
}
public static void main(String[] args) {
Test object = new Test();
}
}
Output:
cssa = 10 b = 20
- Using this() to invoke the current class constructor:
The 'this()' keyword is used to invoke a constructor from another constructor in the same class. This is useful when one constructor has to call another constructor to perform some common tasks.
csharpclass Test {
int a;
int b;
Test() {
this(10, 20);
System.out.println("Inside default constructor");
}
Test(int a, int b) {
this.a = a;
this.b = b;
System.out.println("Inside parameterized constructor");
}
public static void main(String[] args) {
Test object = new Test();
}
}
Output:
scssInside parameterized constructor Inside default constructor
- Using 'this' keyword to return the current class instance:
The 'this' keyword is used to return the current class instance from a method. This is useful when you want to return the instance of the class from the method.
csharpclass Test {
int a;
int b;
Test() {
a = 10;
b = 20;
}
Test get() {
return this;
}
void display() {
System.out.println("a = " + a + " b = " + b);
}
public static void main(String[] args) {
Test object = new Test();
object.get().display();
}
}
Output:
cssa = 10 b = 20
- Using 'this' keyword as a method parameter:
The 'this' keyword is used to pass the current class instance as a parameter to a method. This is useful when passing the current object to another method that needs to access its properties or methods.
For example, in the fourth example of using 'this' keyword as a method parameter, we have a method named 'display' that receives an object of the same class as its parameter. In the 'get' method, we pass the current object (i.e., 'this') as an argument to the 'display' method. Inside the 'display' method, we access the properties of the passed object using the dot operator (i.e., obj.a and obj.b).
Using 'this' as a method parameter is helpful when we need to pass the current object to another method in a class. This approach can help us avoid creating a new object and instead use the existing object to perform operations on it.
Additionally, using 'this' as a method parameter is a cleaner approach as it avoids naming conflicts with other variables or methods that may have the same name. By explicitly passing the current object as a parameter, we can avoid confusion and ensure that we are using the intended object.
Overall, using the 'this' keyword in Java can help us in various scenarios, such as accessing instance variables, invoking class constructors, returning current class instances, invoking current class methods, and passing the current class instance as a parameter to a method.