Java Reference
In-Depth Information
The model of execution
In Sec. 2.7, we discussed execution of method calls, defining the format of a
frame for a method call, introducing the call stack of frames for method calls that
have not yet completed, and giving the sequence of instructions for executing a
method call. You will best understand method calls if you can execute them your-
self, by hand. Only then will you have complete understanding of how calls
work. Further, with this ability, you should have little difficulty understanding
recursive calls later on (see Chap. 15).
We repeat the set of instructions for executing a method call:
The scope box
of a frame is
defined in
Activity 5.1.
Activities 3-7.1
and 3-7.3 use
synched anima-
tion to provide
much better
illustrations of
method calls.
1. Evaluate the arguments of the call and push them onto the call stack.
2. Draw a frame for the call at the top of the call stack; the frame includes
the argument values at the top of the stack. This frame will become the
new active frame .
2(a) Fill in the name of the method and set the program counter to 1.
2(b) Fill in the scope box with the name of the entity in which the method
appears: the name of a folder for a non-static method or constructor, and
the name of the class for a static method.
2(c) Draw the local variables of the method body in the frame.
2(d) Label the argument values pushed onto the call stack in the first step
with the names of the corresponding parameters.
3. Execute the method body. When referencing a name, look in the (new
active) frame for it. If it is not there, look in the item given by the scope
box of the frame.
4. Erase the frame —pop it from the stack. If the method is a function and
the call is terminated by execution of a return statement return e; , push
the value of e onto the call stack.
We now illustrate execution of an assignment statement that includes evalu-
ation of a new-expression, including following the steps in executing its con-
structor call. We assume that the following statement occurs as the first statement
/** An instance has a method to play the clock game. */
public class ClockGame {
/** Constructor: player with fields initialized and a clock window with a clock and a label.
The player information is obtained using a dialog window. The default time is 0:00. */
public ClockGame() { ... }
/** Play until the player terminates the game */
public void playGame() { ... }
Figure 3.16:
Specification of class ClockGame
Search WWH ::

Custom Search