Java Reference
In-Depth Information
ple, we write a method
printSums
, which contains two calls to method
printSum
. Class
PrintExample
, with all three methods discussed in this section,
appears in Fig. 2.1.
Method
printSums
go inside class
PrintExample
. Because
printSum
and
printSums
are defined in the same class, procedure
printSums
can call
printSum
without having to write
PrintExample.printSum(...);
.
At times, you may want to execute parts of a program by hand. In doing so,
there are two different ways that we might execute a call: by
stepping over
it and
by
stepping into
it. Most IDE debuggers have these two possibilities, and it is
essential that you understand the difference.
Stepping over a call
We describe what it means to
step over
a call. Suppose we are executing this
call to procedure
printSums
:
PrintExample.printSums(20, 6);
We have assigned the arguments to the parameters, so that we have this situation:
Java console
b 20
c 6
We are ready to execute the statements in the method body. The first state-
ment is a function call
printSum(b, c)
. We execute it by
stepping over
the call.
To do this, we do what the specification of the procedure says to do: print the
larger of
b
and
c
. So, we place 26 in the Java console, yielding this state:
Java console
26
b
20
c
6
Thus,
stepping over a call
means simply to execute it as an indivisible
action, doing what the specification says to do.
Stepping into a call
Now, the second statement is to be executed:
printSum(b, c * c);
. We
execute this statement using the second method,
stepping into the call
. To do this,
we go through the detailed steps mentioned earlier: assign the arguments of the
call to the parameters of the method and then execute the method body. In this
case, the parameter names are
b
and
c
. To avoid mixing up the parameters
b
and
c
for this new method call with those that already exist, we place them in boxes
as shown below. Each box has in its upper left a subbox that contains the name
of the method called.
Search WWH ::
Custom Search