Java Reference
In-Depth Information
Finally, lab 4 of Lesson 4 of ProgramLive , title “Practice with shapes”, asks
you to modify the picture in several ways.
The final model of execution
In Sec. 3.5, we described the steps in executing a method call. Below, we repeat
the steps, just for completeness. But there is one difference. Now that we have
subclasses, during step 4 (executing the method body), we have to be more care-
ful about what a name refers to when it is referenced in the method body. We
have changed this step slightly to refer to the discussion below the list of steps.
4-3.1, 4-3.2
1. Evaluate the arguments of the call and push them onto the call stack.
2. Draw a frame for the call on 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 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.
Finding the item referenced by a name
Executing the method body (step 3 above) generally requires finding the
variable to which a name refers or finding a method to which a method call
refers. When dealing just with classes (but not subclasses), this task is rather sim-
ple: look in the active frame, and if it is not there, look in the folder or file draw-
er given by the scope box of the frame. Now that we have introduced subclass-
es, the problem of finding a variable or method is more complicated. So we give
a detailed explanation.
Remember that the program being executed is syntactically correct (if not, it
could not be executed) and that it is known whether a variable is a parameter,
local variable, static variable, or instance variable, and similar information is
known for the method m of a method call m( args ) . Also, a reference to a variable
has the form x or expression .x , where x is a variable name and expression is
Search WWH ::

Custom Search