Java Reference
In-Depth Information
of static method m in a class C and that class Coordinates is given in Fig. 3.5:
Coordinates d= new Coordinates(5, 6 + 2);
Part (a) of Fig. 3.17 shows the call stack with the frame for the call to method m
at its top. The program counter is 1 because statement 1 is to be executed. The
scope box of the frame contains the name C of the class, since m is static. The
frame contains variable d .
Step 1 in evaluating the new-expression is to create a folder of class
Coordinates . It is shown below. The fields have the default value 0 since their
declarations do not have initializing assignments. We have arbitrarily labeled the
folder a9 . This folder goes in file drawer for class Coordinates .
a9
Coordinates
Coordinates( int , int )
toString()
x0
y0
Step 2 in evaluating the new-expression is to execute the constructor call
Coordinates(5, 6+2) , where the method is in folder a9 :
1. Step 1 is to evaluate the arguments and push their values on the call stack.
This result of this step is down in part (b) of Fig. 3.17.
2. Step 2 is to create the frame for the call to Coordinates . We have done
this in part (c) of Fig. 3.17. The scope box contains the name of the fold-
er in which the called constructor appears. There are no local variables,
so none are drawn. The argument values that were pushed onto the stack
become the parameters. The result of this step is shown in Fig. 3.17(d).
3. Step 3 is to execute the body of the constructor. This consists of execut-
ing the assignments to x and y . Where are variables x and y ? Since they
are not in the frame for the call, the scope box is used to determine where
they are —in folder a9 . Thus, folder a9 is changed to look as shown
below, and the frame for the call is changed as in Fig. 3.17(e) —the pro-
gram counter has been changed to 3.
a9
Coordinates
Coordinates( int , int )
toString()
x5
y8
4. Step 4 of executing the constructor call is to erase the frame for the call.
Execution of the call is finished. The state of affairs is as shown in Fig.
3.17(f).
Search WWH ::




Custom Search