Summary: in this tutorial, you will learn about Java this
keyword, and how to use it effectively in your classes.
Introduction to Java this keyword
A class is a template for creating objects. To refer to the current object inside the class, you use the this
keyword. In other words, the this
keyword is a reference to the current instance of the class.
Since the this
keyword references the current instance of the class, you only can use it within the methods of the class.
Suppose you have a Person
class with two properties name
and age
, and a method sayHi()
:
class Person {
String name;
int age;
void sayHi() {
System.out.printf("Hi. It's %s.", name);
}
}
Code language: Java (java)
Because this
references the current instance of the Person
class, you can explicitly access the name
property in the sayHi()
method like this:
this.name;
Code language: Java (java)
The Person
class will become:
class Person {
String name;
int age;
void sayHi() {
System.out.printf("Hi. It's %s.", this.name);
}
}
Code language: Java (java)
When using Java this keyword
The this
keyword is useful in some scenarios.
1) Differentiating between property and method parameters with the same name
You can the this
keyword to differentiate between properties and method parameters that have the same name.
Suppose you want to define a method that sets the new name for a person object:
class Person {
String name;
int age;
void sayHi() {
System.out.printf("Hi. It's %s.", this.name);
}
void setName(String name) {
// ...
}
}
Code language: Java (java)
In this example, the setName()
method has the name
parameter which is the same as the name
property of the Person
class.
How do you reference the name
property inside the setName()
method? To do that, you use the this
keyword as follows:
class Person {
String name;
int age;
void sayHi() {
System.out.printf("Hi. It's %s.", this.name);
}
void setName(String name) {
this.name = name;
}
}
Code language: Java (java)
In the setName()
method, this.name
references the name
property of the current Person
object while the name
references the method parameter.
Similarly, you can add the setAge()
method using the this
keyword to reference the age
property of the current object:
class Person {
String name;
int age;
void sayHi() {
System.out.printf("Hi. It's %s.", this.name);
}
void setName(String name) {
this.name = name;
}
void setAge(int age) {
this.age = age;
}
}
Code language: Java (java)
2) Chaining methods
Because the this
keyword references the current object, you can return it from a method, allowing for method chaining.
For example, we can modify the setName()
and setAge()
method to return a Person
object and add a statement that returns this
:
class Person {
String name;
int age;
void sayHi() {
System.out.printf("Hi. It's %s.", this.name);
}
Person setName(String name) {
this.name = name;
return this;
}
Person setAge(int age) {
this.age = age;
return this;
}
}
Code language: Java (java)
The following creates the person
object and calls setName()
, setAge()
and sayHi()
methods:
public class App {
public static void main(String[] args) {
var person = new Person();
person.setName("John");
person.setAge(22);
person.sayHi();
}
}
Code language: Java (java)
Since the setName()
and setAge()
method returns the current Person
object, you can chain the method calls like this:
public class App {
public static void main(String[] args) {
var person = new Person();
person.setName("John")
.setAge(22)
.sayHi();
}
}
Code language: Java (java)
Summary
- The Java
this
keyword references the current instance of a class. - Use Java
this
keyword to differentiate between properties and method parameters that have the same names and to enable method chaining.