3.5 Instance Methods and the Object Reference this
Instance methods belong to every object of the class and can be invoked only on objects. All members defined in the class, both static and non-static, are accessible in the context of an instance method. The reason is that all instance methods are passed an implicit reference to the current object—that is, the object on which the method is being invoked.
The current object can be referenced in the body of the instance method by the keyword this. In the body of the method, the this reference can be used like any other object reference to access members of the object. In fact, the keyword this can be used in any non-static context. The this reference can be used as a normal reference to reference the current object, but the reference cannot be modified—it is a final reference (§5.5, p. 225).
The this reference to the current object is useful in situations where a local variable hides, or shadows, a field with the same name. In Example 3.4, the two parameters noOfWatts and indicator in the constructor of the Light class have the same names as the fields in the class. The example also declares a local variable location, which has the same name as one of the fields. The reference this can be used to distinguish the fields from the local variables. At (1), the this reference is used to identify the field noOfWatts, which is assigned the value of the parameter noOfWatts. Without the this reference at (2), the value of the parameter indicator is assigned back to this parameter, and not to the field by the same name, resulting in a logical error. Similarly at (3), without the this reference, it is the local variable location that is assigned the value of the parameter site, and not the field with the same name.
Example 3.4 Using the this Reference
public class Light {
// Fields:
int noOfWatts; // Wattage
boolean indicator; // On or off
String location; // Placement
// Constructor:
public Light(int noOfWatts, boolean indicator, String site) {
String location;
this.noOfWatts = noOfWatts; // (1) Assignment to field
indicator = indicator; // (2) Assignment to parameter
location = site; // (3) Assignment to local variable
this.superfluous(); // (4)
superfluous(); // equivalent to call at (4)
}
// Instance method:
public void superfluous() {
System.out.printf(“Current object: %s%n”, this); // (5)
}
// Static method:
public static void main(String[] args) {
Light light = new Light(100, true, “loft”);
System.out.println(“No. of watts: ” + light.noOfWatts);
System.out.println(“Indicator: ” + light.indicator);
System.out.println(“Location: ” + light.location);
}
}
Probable output from the program:
Current object: Light@1bc4459
Current object: Light@1bc4459
No. of watts: 100
Indicator: false
Location: null
If a member is not shadowed by a local declaration, the simple name member is considered a shorthand notation for this.member. In particular, the this reference can be used explicitly to invoke other methods in the class. This usage is illustrated at (4) in Example 3.4, where the method superfluous() is called.
If, for some reason, a method needs to pass the current object to another method, it can do so using the this reference. This approach is illustrated at (5) in Example 3.4, where the current object is passed to the printf() method. The printf() method prints the text representation of the current object (which comprises the name of the class of the current object and the hexadecimal representation of the current object’s hash code). The hash code of an object is an int value that uniquely identifies the object.
Note that the this reference cannot be used in a static context, as static code is not executed in the context of any object.