document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1.  
  2. /**
  3.  * Fig. 8.4: ThisTest
  4.  *
  5.  * @author Mohammad Tauchid
  6.  * @version 0.1
  7.  */
  8. public class ThisTest
  9. {
  10.     public static void main (String[] args) {
  11.         SimpleTime time = new SimpleTime(15, 30, 19);
  12.         System.out.println(time.buildString());
  13.     }
  14. }
  15.  
  16. // class SimpleTime demonstrate the "this" reference
  17. class SimpleTime {
  18.     private int hour;   // 0 - 23
  19.     private int minute; // 0 - 59
  20.     private int second; // 0 - 59
  21.    
  22.     // if the constructor uses parameter names identical to
  23.     // instance variable names the "this" reference is
  24.     // required to distinguish between the names
  25.     public SimpleTime (int hour, int minute, int second) {
  26.         this.hour = hour;       // set "this" object\'s hour
  27.         this.minute = minute;   // set "this" object\'s minute
  28.         this.second = second;   // set "this" object\'s second
  29.     }
  30.    
  31.     // use explicit and implicit "this" to call toUniversalString
  32.     public String buildString () {
  33.         return String.format("%24s: %s\\n%24s: %s",
  34.             "this.toUniversalString()", this.toUniversalString(),
  35.             "toUniversalString()", toUniversalString());
  36.     }
  37.    
  38.     // convert to String in universal-time format (HH:MM:SS)
  39.     public String toUniversalString() {
  40.         // "this" is not required here to access instance variables,
  41.         // because method does not have local variables with same
  42.         // name as instance variables
  43.         return String.format("%02d:%02d:%02d",
  44.             this.hour, this.minute, this.second);
  45.     }
  46. }
');