
“this” Keyword
All instance methods have automatic access to other instance methods and any data (instance variables)
defined for the object. In the example below, the pimpMyRide method calls the getDescription() method and
uses Car instance variables.
public class Car {
String color;
String type;
{
color="red";
type="sedan";
}
String getDescription(){
String desc = "This is a " + color + " " + type;
return desc;
}
void pimpMyRide(String newColor, String customized) {
color = newColor;
type = customized + " " + type;
System.out.println(getDescription());
}
}
Within an instance method or a constructor, the keyword this is a reference to the current object. In other
words, this refers to the current instance. You can use this to refer to any instance variable or instance method
of the object from within an instance method or a constructor.
Rewriting the code above should help demonstrate this.
public class Car {
String color;
String type;
{
this.color="red";
this.type="sedan";
}
String getDescription(){
String desc = "This is a " + this.color + " " + this.type;
return desc;
}
void pimpMyRide(String newColor, String customized) {
this.color = newColor;
this.type = customized + " " + this.type;
System.out.println(this.getDescription());
}
}
Even though this code works, the use of this here doesn’t seem to make much sense? In fact, it might just
make things more complex. However, consider the pimpMyRide method if it were written as shown below.
void pimpMyRide(String color, String type) {
color = color; //??? Which color is which
type = type + " " + type; //??? Which type is which
System.out.println(getDescription());
}
The parameters passed into pimpMyRide now conflict with the instance variable names. This code will
compile, but it won’t run correctly, as will be discussed. When parameters passed into a constructor or
method have the same name as instance variables, this is called shadowing a field. Shadowing helps clarify
how the parameter will be used to set or modify an instance variable.
To fix the problem, the this keyword helps to disambiguate what is the object’s instance variable and what is
just the parameter.
void pimpMyRide(String color, String type) {
this.color = color;
this.type = type + " " + this.type;
System.out.println(getDescription());
}
Static methods do not have access to this because when you enter a static method, you are not in an object
instance. “this” only applies to instances.
In the above case, this helps provide clarity, but if alternative parameter names are used, you can avoid having
to use this.
In another case, the this keyword is the only way to accomplish the task. Consider the Car example with two
constructors as specified below.
Car(){
carCount++;
serialNumber = carCount;
}
Car(String c, String t){
carCount++;
serialNumber = carCount;
color = c;
type = t;
}
Do you notice some similarities between the two constructors? What happens if the way serial numbers are
handled is changed? In this case, you would have to modify two constructors where duplicate code is used to
deal with serial numbers. This is not a very good reuse design!
How can the common code be isolated and reused in these constructors? Constructors can call other
constructors in the same class using this. Using this is similar to calling a method from within your
constructor: you must match the appropriate argument list. To call another constructor, use this on the first
line of another constructor.
Car(){
carCount++;
serialNumber = carCount;
}
Car(String c, String t){
this();
color = c;
type = t;
}
By having the second constructor call the first one, you don't need to repeat code. Any change made to the
first constructor will also affect the second constructor.
Setting up one constructor to call another is called constructor chaining.
Using the this keyword is awkward at first, but there are only two uses for it:
• Referring to the current instance (e.g., this.color or this.getDescription() )
• Referring to another constructor (e.g., this(<arg>) ) from inside a constructor.
this
Table of Contents
Copyright (c) 2008. Intertech, Inc. All Rights Reserved. This information is to be used exclusively as an
online learning aid. Any attempts to copy, reproduce, or use for training is strictly prohibited.
Courseware
Training Resources
Tutorials
Services