document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /**
  2.  * This used implicitly and explicitly to refer to members of an object.
  3.  *
  4.  * @author Muthia Qurrota Akyun
  5.  * 9 Oktober 2020
  6.  */
  7.  
  8. public class ThisTest  
  9.  {  
  10.    public static void main (String[] args)  
  11.    {  
  12.      SimpleTime time = new SimpleTime( 15, 30, 19 );  
  13.      System.out.println (time.buildString());  
  14.    }  
  15.  }  
  16.    
  17.  class SimpleTime  
  18.  {  
  19.    private int hour;  
  20.    private int minute;  
  21.    private int second;  
  22.      
  23.    //if the contructor uses parameter names identical to instance variable names  
  24.    //the "this" reference is required to distinguish between the names  
  25.    public SimpleTime( int hour, int minute, int second )  
  26.    {  
  27.      this.hour = hour; // set "this" object\'s hour  
  28.      this.minute = minute; //set "this" object\'s minute  
  29.      this.second = second; //set "this" object\'s second  
  30.    }  
  31.      
  32.    //use explicit and implicit "this" to call toUniversalString  
  33.    public String buildString()  
  34.    {  
  35.      return String.format ("%24s: %s\\n%24s: %s", "this.toUnivesalString()", this.toUniversalString(), "toUniversalString()", toUniversalString() );  
  36.    }  
  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 names as instance variables  
  43.      return String.format("%02d:%02d:%02d", this.hour, this.minute, this.second);  
  44.    }  
  45.  }  
');