Java Reference
In-Depth Information
Exercise 3.28 Look at the second constructor in ClockDisplay 's source code. Explain
what it does and how it does it.
Exercise 3.29 Identify the similarities and differences between the two constructors. Why is
there no call to updateDisplay in the second constructor, for instance?
3.11
Method calls
3.11.1
Internal method calls
The last line of the first ClockDisplay constructor consists of the statement
updateDisplay();
This statement is a method call. As we have seen above, the ClockDisplay class has a method
with the following signature:
Concept:
Methods can call
other methods of
the same class as
part of their imple-
mentation. This is
called an internal
method call .
private void updateDisplay()
The method call above invokes this method. Because this method is in the same class as the
call of the method, we also call it an internal method call. Internal method calls have the syntax
methodName ( parameter-list )
In our example, the method does not have any parameters, so the parameter list is empty. This is
signified by the pair of parentheses with nothing between them.
When a method call is encountered, the matching method is executed, and then execution re-
turns to the method call and continues at the next statement after the call. For a method signa-
ture to match the method call, both the name and the parameter list of the method must match.
Here, both parameter lists are empty, so they match. This need to match against both method
name and parameter lists is important, because there may be more than one method of the same
name in a class—if that method is overloaded.
In our example, the purpose of this method call is to update the display string. After the two num-
ber displays have been created, the display string is set to show the time indicated by the number
display objects. The implementation of the updateDisplay method will be discussed below.
3.11.2
External method calls
Now let us examine the next method: timeTick . The definition is:
public void timeTick()
{
minutes.increment();
if(minutes.getValue() == 0) { // it just rolled over!
hours.increment();
}
updateDisplay();
}
 
 
Search WWH ::




Custom Search