students are introduced to the
Note that we pass
Here we've introduced a new variable called
But here we encounter a problem. We now have two variables with the same name. One
Java assumes you mean the closest
Thus,
Here we have first provided a constructor with two parameters:
Later we decide to add another constructor with three parameters, adding height to the existing age and weight parameters. You could write the new constructor like this:
But instead of repeating existing code in this constructor, you can use the
It's as if we're saying to the three-parameter constructor:
this
keyword literally from the first lessons. Over time, its meaning becomes clear. But looking back, many people probably admit to themselves that for a long time they couldn't understand the Zen of this keyword. This article will pull back the curtain covering the secrets of the this
keyword for anyone who has not yet been able to do so...
I've you've got Schildt's Java reference, then on page 171 you can see that the this
keyword is required for a method to reference the object that called it. We could end the lesson with that. But we need specifics.
As a rule, you need to use this
in two cases:
- When an instance variable and method/constructor variable have the same name;
- When you need to call a specific type of constructor (for example, a default constructor or parameterized constructor) from another type of constructor. This is also called an explicit constructor call.
Example 1: An instance variable and method variable have the same name.
Suppose we have aHuman
class that defines a name field:
Let's create a setter for the name
variable (the setter is fully functional — there's no catch here):
Note that we pass
String newName
to the setName
setter method We declared a new variable and could have named it whatever we want because it will be visible only within the curly braces ({}) that enclose the setName
method. Note that the setter has a single line:
Here we've introduced a new variable called
newName
and assigned it to the object's existing name
variable. Many programmers might find it odd to introduce a variable with a new name when ultimately we are talking about the same thing. That is, we're talking about the name field in the Human
class. That's why Java's creators thought up a way to conveniently use the same variable name. In other words, why have two names for a variable denoting the same thing. In other words, we want to do something like this:
But here we encounter a problem. We now have two variables with the same name. One
String name
belongs to the Human
class, while the other String name
belongs to its setName
method. As a result, the JVM wouldn't know which variable you're referring to when you write the following line in the setter:
Java assumes you mean the closest
name
variable, i.e. the one from the setName
method:
So it turns out that you're simply assigning the method's name
variable to itself. Which of course doesn't make any sense. Therefore, the language needed some way to distinguish the Human
class's name
variable from the name
variable in the setName
method. This problem was solved by introducing the this
keyword, which in this case indicates that you intend to reference the variable associated with an instancen of the Human
class, not the variable in the method:
In other words, this
refers to the calling object, as we mentioned at the beginning of the article. As a result, the setName
method sets the person's name on the created object. Below is the program's code without using the this
keyword. The code creates a Human
object and assigns a name to it:
And here is the code with the this
keyword:
Thus,
this
lets us avoid introducing new variables to denote the same thing, making the code cleaner and less cluttered with extra variables.
Example 2: Using this for an explicit constructor call
Calling one constructor from another can be useful when you have (oddly enough) multiple constructors and you don't want to the new constructor to duplicate initialization code previously written in a different constructor. Confused? It's not so scary as it seems. Look at the code below. It has two constructors for theHuman
class:
Here we have first provided a constructor with two parameters:
int age
and int weight
. Suppose it has two lines of code:
Later we decide to add another constructor with three parameters, adding height to the existing age and weight parameters. You could write the new constructor like this:
But instead of repeating existing code in this constructor, you can use the
this
keyword to explicitly call the constructor with two parameters:
It's as if we're saying to the three-parameter constructor:
- call this other constructor that has two parameters
- and then add another variable.
this
is passed implicitly to all non-static methods (that's why this
is often called an implicit parameter) and can be used to refer to the object that called the method. Don't be afraid of this keyword, because this
is not scary.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.