-2

I know the meaning for "this" keyword.. we use it to reference instance variables if we have a method arguments that has the same name as instance variables but in this code i don't understand why we use it.. what does " this( 0, 0, 0 ) " even mean? we can just put arguments inside the instructors. Can someone please explain?

public class Time2
  {
          private int hour; // 0 - 23
          private int minute; // 0 - 59
          private int second; // 0 - 59

          // Time2 no-argument constructor: initializes each instance variable
          // to zero; ensures that Time2 objects start in a consistent state
          public Time2()
                  {
                  this( 0, 0, 0 ); // invoke Time2 constructor with three arguments
                  } // end Time2 no-argument constructor

          // Time2 constructor: hour supplied, minute and second defaulted to 0
          public Time2( int h )
                  {
                  this ( h, 0, 0 ); // invoke Time2 constructor with three arguments
                  } // end Time2 one-argument constructor

          // Time2 constructor: hour and minute supplied, second defaulted to 0
          public Time2( int h, int m )
                  {
                  this( h, m, 0 ); // invoke Time2 constructor with three arguments
                  } // end Time2 two-argument constructor

          // Time2 constructor: hour, minute and second supplied
          public Time2( int h, int m, int s )
                  {
                  setTime( h, m, s ); // invoke setTime to validate time
                  } // end Time2 three-argument constructor

          // Time2 constructor: another Time2 object supplied
          public Time2( Time2 time )
                  {
                  // invoke Time2 three-argument constructor
                  this( time.getHour(), time.getMinute(), time.getSecond() );
                  } // end Time2 constructor with a Time2 object argument

          // Set Methods
          // set a new time value using universal time; ensure that
          // the data remains consistent by setting invalid values to zero
          public void setTime( int h, int m, int s )
                  {
                  setHour( h ); // set the hour
                  setMinute( m ); // set the minute
                  setSecond( s ); // set the second
                  } // end method setTime

          // validate and set hour
          public void setHour( int h )
                  {
                  hour = ( ( h >= 0 && h < 24 ) ? h : 0 );
                  } // end method setHour

                  // validate and set minute
                   public void setMinute( int m )
                   {
                   minute = ( ( m >= 0 && m < 60 ) ? m : 0 );
                  } // end method setMinute
                   public void setSecond( int s )
                  {
                   second = ( ( s >= 0 && s < 60 ) ? s : 0 );
                   } // end method setSecond

                  // Get Methods
                   // get hour value
                   public int getHour()
                   {
                   return hour;
                   } // end method getHour

                   // get minute value
                   public int getMinute()
                   {
                   return minute;
                   } // end method getMinute

                   // get second value
                   public int getSecond()
                   {
                   return second;
                   } // end method getSecond

                   // convert to String in universal-time format (HH:MM:SS)
                   public String toUniversalString()
                   {
                   return String.format(
                   "%02d:%02d:%02d", getHour(), getMinute(), getSecond() );
                   } // end method toUniversalString

                   // convert to String in standard-time format (H:MM:SS AM or PM)
                   public String toString()
                   {
                   return String.format( "%d:%02d:%02d %s",
                   ( (getHour() == 0 || getHour() == 12) ? 12 : getHour() % 12 ),
                   getMinute(), getSecond(), ( getHour() < 12 ? "AM" : "PM" ) );
                   } // end method toString
                   } // end class Time2ere
Jens
  • 67,715
  • 15
  • 98
  • 113
Muath
  • 11
  • 3

2 Answers2

1

this( 0, 0, 0 ) call an other constructor with 3 parameters

so

public Time2()
{
    this( 0, 0, 0 ); // invoke Time2 constructor with three arguments
} // end Time2 no-argument constructor

calls:

public Time2( int h, int m, int s )
{
    setTime( h, m, s ); // invoke setTime to validate time
} // end Time2 three-argument constructor

with h,m,s = 0

Jens
  • 67,715
  • 15
  • 98
  • 113
  • As an extra bit of information for OP, this is similar to when you call something such as `super(0,0,0)`, which instead would call the _parent_ class's constructor with the matching parameters, which you may have seen before. – Nexevis Jan 14 '20 at 18:45
0

You are simply calling the constructor from within itself.

Let's imagine we have a class Ball:

public class Ball {
  private Color color;
  private int size;

  // This is our constructor
  public Ball(Color c, int s) {
    this.color = c;
    this.size = s;
  }
}

Now, if we wanted to create a new red ball with size 4, we could do so with new Ball(Color.RED, 4);

this refers to the current object. So if we are within Ball and creating a new Ball, we would do this(Color.RED, 4);.

This is effectively like saying new Ball();.

sleepToken
  • 1,866
  • 1
  • 14
  • 23