Summary: In this tutorial, you will learn about this keyword in Java. You will also get to know when and how to use them in your Java codes.
this in Java
The this
keyword in Java refers to the current instance of an object. It can be used to refer to the current object’s instance variables, or to invoke other methods on the current object.
For example, consider the following class:
class MyClass {
int x;
public MyClass(int x) {
this.x = x;
}
public void setX(int x) {
this.x = x;
}
public int getX() {
return this.x;
}
}
Here, this
is used to distinguish the instance variable x
from the method argument x
in the setX
method. The this
keyword can also be used to invoke other methods on the current object, for example:
class MyClass {
int x;
public MyClass(int x) {
this.x = x;
}
public void setX(int x) {
this.setX(x);
}
}
In this case, the setX
method is calling itself using the this
keyword.
What is the use of this Keyword?
The this
keyword is used for several purposes in Java:
1. To refer to the current object’s instance variables
class MyClass {
int x;
public MyClass(int x) {
this.x = x;
}
public void setX(int x) {
this.x = x; // this.x refers to the instance variable x of the current object
}
public int getX() {
return this.x; // this.x refers to the instance variable x of the current object
}
}
In this example, the this
keyword is used to distinguish between the instance variable x
and the method argument x
in the setX
method. The instance variable is referred to using this.x
, while the method argument is simply referred to as x
.
2. To invoke other methods on the current object
class MyClass {
int x;
public MyClass(int x) {
this.x = x;
}
public void setX(int x) {
this.setX(x); // this.setX(x) invokes the setX method on the current object
}
}
Here, the setX
method is calling itself using the this
keyword. This can be useful in certain cases, for example when the method needs to perform some operation before setting the value of x
.
3. To pass the current object as an argument
class MyClass {
int x;
public MyClass(int x) {
this.x = x;
}
public void process() {
Processor.process(this); // this is passed as an argument to the process method
}
}
class Processor {
static void process(MyClass obj) {
// ...
}
}
In this example, the this
keyword is used to pass the current object as an argument to the process
method. This allows the process
method to operate on the current object.
4. To return the current object from a method
class MyClass {
int x;
public MyClass(int x) {
this.x = x;
}
public MyClass copy() {
return this; // return the current object
}
}
Here, the this
keyword is used to return the current object from the copy
method. This allows the caller to chain method calls on the returned object, for example: obj.copy().setX(10)
.
this Keyword in Constructor Overloading
The this
keyword can also be used in the context of constructor overloading in Java.
Constructor overloading is the practice of having multiple constructors with different parameter lists in a single class.
Here is an example of a class with two overloaded constructors:
class MyClass {
int x;
int y;
public MyClass(int x) {
this.x = x;
this.y = 0;
}
public MyClass(int x, int y) {
this.x = x;
this.y = y;
}
}
In the first constructor, the this
keyword is used to distinguish the instance variable x
from the method argument x
. In the second constructor, the this
keyword is used to set the values of both instance variables x
and y
.
The this
keyword can also be used to invoke one constructor from another in the same class. For example:
class MyClass {
int x;
int y;
public MyClass(int x) {
this(x, 0); // invoke the second constructor
}
public MyClass(int x, int y) {
this.x = x;
this.y = y;
}
}
In this case, the first constructor calls the second constructor using the this
keyword, passing the value of the x
argument as a parameter.
This allows the first constructor to reuse the code in the second constructor to set the values of both instance variables.