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