Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

F. Classes and Objects: A Deeper Look > F.4. Referring to the Current Object’s ...

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.


 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( "%24s: %s\n%24s: %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( "%02d:%02d:%02d",
45            this.hour, this.minute, this.second );
46      } // end method toUniversalString
47   } // end class SimpleTime


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial