At JavaRush, students are introduced to the keyword literally from the first lectures
and it turns out that you simply assign a value to a variable
That is,
And below is the program code with the keyword
this
. And over time it becomes clear what it means. But many, looking back, will probably honestly say to themselves that for a long time they could not realize the zen of this key word. In this article we lift the veil of secrets about using keywords this
for those who still can’t figure it out... Welcome! If you pick up Schildt's Java reference book , on page 171 you'll read that a keyword this
is required so that a method can reference the object that called it. This could actually be the end of it. But we need specifics. As a rule, this
it should be used in two cases:
- When a class instance variable and a method/constructor variable have the same name;
- When you need to call a constructor of one type (for example, a default or parameterized constructor) from another. This is also called an explicit constructor call.
Example one - instance variable and method variable have the same name
Let's say we have a classHuman
for which the "name" field is defined: Let's name
create a setter for the variable (the setter is quite working and there is no trick here): Please note that we are passing the variable to the method (setter) . We introduced a new variable and (in general) could call it whatever we wanted because it would only be visible within the {curly braces} of the method . Notice that there is one line in the setter: setName
String newName
setName
name = newName;
That is, in fact, we introduced a new variable newName
and assigned it to a variable already existing in the class name
. Many programmers thought it strange to introduce a variable with a new name if in the end we are talking about the same thing. About the name in the class Human
. Therefore, the language developers thought about making it convenient to use one variable name. In other words, why have two names for a variable that means the same thing. That is, I would like to do something like this: But in this case a problem arises . We now have two variables that are named the same. One String name
belongs to the class Human
, and the other String name
to its method setName
. Therefore, the Java machine does not know which variable you mean when you write a string in a setter:
name = name;
Java takes the closest one - name
from the method setName
:
name
from this method, to it. Which of course doesn't make any sense. Therefore, some way was needed to distinguish a variable name
from a class Human
from a variable name
from a method setName
. To solve this problem, the keyword was introduced this
, which in this case will indicate that it is necessary to call a variable not of a method, but of a class Human
:
this
it will refer to the calling object, as was said at the beginning of the article. As a result, the name of the person setName
will be set through the setter to the created object. Below is the code without using the keyword this
. The code creates a class object Human
and gives it a name:
this
:
public class Solution{
public static void main(String[] args) {
Human human1 = new Human();
human1.setName("Volodya");
human1.print();
}
}
class Human{
String name;
public String getName() {
return name;
}
public void setName(String name){
this.name = name;
}
void print(){
System.out.println(name);
}
}
Thus, it this
avoids introducing new variables to denote the same thing, which makes the code less “overloaded” with additional variables.
Example Two - Using this to explicitly call a constructor
Calling one constructor from another can be useful when you (oddly enough) have several constructors and you do not want to rewrite the initialization code given earlier in the constructor in a new constructor. Confused? Everything is not as scary as it seems. Look at the code below, it has two class constructorsHuman
:
class Human{
int age;
int weight;
int height;
Human(int age, int weight){
this.age = age;
this.weight = weight;
}
Human(int age, int weight, int height){
//you call the constructor with two parameters
this(age, weight);
//and add the missing variable
this.height = height;
}
}
Here we first have a two-parameter constructor that accepts int age
and int weight
. Let's say we wrote two lines of code in it:
this.age = age;
this.weight = weight;
and then they decided to add another constructor, with three parameters, which, in addition to age and weight, also takes height. In the new constructor you could write this:
this.age = age;
this.weight = weight;
this.height = height;
But instead of repeating the code you've already written in this constructor, you can use a keyword to this
explicitly call the constructor with two parameters:
this(age, weight);
// and add the missing variable:
this.height = height;
So you're kind of telling the constructor with three parameters:
- call this constructor, which has two parameters.
- and add the missing variable.
this
is passed to all methods except static ones (hence why it this
is often called an implicit parameter) and can be used to refer to the object that called the method. There is no need to be afraid of this keyword, because This
it is not scary.
GO TO FULL VERSION