Java Reference
In-Depth Information
We can now use the
Step
button repeatedly to step to the end of the method. This allows us to
see the path the execution takes. This is especially interesting in conditional statements: we can
clearly see which branch of an if statement is executed and use this to see whether it matches
our expectations.
Exercise 3.39
Call the same method (
printNextMailItem
) again. Step through the
method again, as before. What do you observe? Explain why this is.
3.13.3
Stepping into methods
When stepping through the
printNextMailItem
method, we have seen two method calls
to objects of our own classes. The line
MailItem item = server.getNextMailItem(user);
includes a call to the
getNextMailItem
method of the
server
object. Checking the instance
variable declarations, we can see that the
server
object is declared of class
MailServer
.
The line
item.print();
calls the
print
method of the
item
object. We can see in the first line of the
printNextMail
Item
method that
item
is declared to be of class
MailItem
.
Using the
Step
command in the debugger, we have used abstraction: we have viewed the
print
method of the
item
class as a single instruction, and we could observe that its effect is to print
out the details (sender, recipient, and message) of the mail item.
If we are interested in more detail, we can look further into the process and see the print method
itself execute step by step. This is done by using the
Step Into
command in the debugger instead
of the
Step
command.
Step Into
will step into the method being called and stop at the first line
inside that method.
Exercise 3.40
Set up the same test situation as we did before. That is, send a message
from Sophie to Juan. Then invoke the
printNextMailItem
message of Juan's mail client
again. Step forward as before. This time, when you reach the line
item.print();
use the
Step Into
command instead of the
Step
command. Make sure you can see the text
terminal window as you step forward. What do you observe? Explain what you see.
3.14
Method calling revisited
In the experiments in Section 3.13, we have seen another example of object interaction
similar to one we saw before: objects calling methods of other objects. In the
printNext-
MailItem
method, the
MailClient
object made a call to a
MailServer
object to retrieve
the next mail item. This method (
getNextMailItem
) returned a value—an object of type
Search WWH ::
Custom Search