F.4. Referring to the Current Object’s Members with the this Reference

Every object can access a reference to itself with keyword this (sometimes called the this reference). When a non-static method is called for a particular object, the method’s body implicitly uses keyword this to refer to the object’s instance variables and other methods. This enables the class’s code to know which object should be manipulated. As you’ll see in Fig. F.4, you can also use keyword this explicitly in a non-static method’s body. Section F.5 shows another interesting use of keyword this. Section F.10 explains why keyword this cannot be used in a static method.

We now demonstrate implicit and explicit use of the this reference (Fig. F.4). This example is the first in which we declare two classes in one file—class ThisTest is declared in lines 4–11, and class SimpleTime in lines 14–47. We do this to demonstrate that when you compile a .java file containing more than one class, the compiler produces a separate class file with the .class extension for every compiled class. In this case, two separate files are produced—SimpleTime.class and ThisTest.class. When one source-code (.java) file contains multiple class declarations, the compiler places both class files for those classes in the same directory. Note also in Fig. F.4 that only class ThisTest is declared public. A source-code file can contain only one public class—otherwise, a compilation error occurs. Non-public classes can be used only by other classes in the same package. So, in this example, class SimpleTime can be used only by class ThisTest.


 1   // Fig. F.4: ThisTest.java
 2   // this used implicitly and explicitly to refer to members of an object.
 3
 4   public class ThisTest
 5   {
 6      public static void main( String[] args )
 7      {
 8         SimpleTime time = new SimpleTime( 15, 30, 19 );
 9         System.out.println( time.buildString() );
10      } // end main
11   } // end class ThisTest
12
13   // class SimpleTime demonstrates the "this" reference
14   class SimpleTime
15   {
16      private int hour; // 0-23
17      private int minute; // 0-59
18      private int second; // 0-59
19
20      // if the constructor uses parameter names identical to
21      // instance variable names the "this" reference is
22      // required to distinguish between the names
23      public SimpleTime( int hour, int minute, int second )
24      {
25         this.hour = hour; // set "this" object's hour      
26         this.minute = minute; // set "this" object's minute
27         this.second = second; // set "this" object's second
28      } // end SimpleTime constructor
29
30      // use explicit and implicit "this" to call toUniversalString
31      public String buildString()
32      {
33         return String.format( "_s: %s _s: %s",
34            "this.toUniversalString()", this.toUniversalString(),
35            "toUniversalString()", toUniversalString() );
36      } // end method buildString
37
38      // convert to String in universal-time format (HH:MM:SS)
39      public String toUniversalString()
40      {
41         // "this" is not required here to access instance variables,
42         // because method does not have local variables with same
43         // names as instance variables
44         return String.format( "_d:_d:_d",
45            this.hour, this.minute, this.second );
46      } // end method toUniversalString
47   } // end class SimpleTime

this.toUniversalString(): 15:30:19
     toUniversalString(): 15:30:19


Fig. F.4 | this used implicitly and explicitly to refer to members of an object.

Class SimpleTime (lines 14–47) declares three private instance variables—hour, minute and second (lines 16–18). The constructor (lines 23–28) receives three int arguments to initialize a SimpleTime object. We used parameter names for the constructor (line 23) that are identical to the class’s instance-variable names (lines 16–18). We don’t recommend this practice, but we did it here to shadow (hide) the corresponding instance variables so that we could illustrate a case in which explicit use of the this reference is required. If a method contains a local variable with the same name as a field, that method will refer to the local variable rather than the field. In this case, the local variable shadows the field in the method’s scope. However, the method can use the this reference to refer to the shadowed field explicitly, as shown on the left sides of the assignments in lines 25–27 for SimpleTime’s shadowed instance variables.

Method buildString (lines 31–36) returns a String created by a statement that uses the this reference explicitly and implicitly. Line 34 uses it explicitly to call method toUniversalString. Line 35 uses it implicitly to call the same method. Both lines perform the same task. You typically will not use this explicitly to reference other methods within the current object. Also, line 45 in method toUniversalString explicitly uses the this reference to access each instance variable. This is not necessary here, because the method does not have any local variables that shadow the instance variables of the class.

Application class ThisTest (lines 4–11) demonstrates class SimpleTime. Line 8 creates an instance of class SimpleTime and invokes its constructor. Line 9 invokes the object’s buildString method, then displays the results.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset