Java Reference
In-Depth Information
Sometimes there is one name that perfectly describes the use of a variable—it fits so well that
we do not want to invent a different name for it. We want to use it for the parameter, where it
serves as a hint to the caller, indicating what needs to be passed; and we want to use it for the
field, where it is useful as a reminder for the implementer of the class, indicating what the field
is used for. If one name perfectly describes the use, it is reasonable to use it for both and to go
through the trouble of using the this keyword in the assignment to resolve the name conflict.
3.13
Using a debugger
The most interesting class in the mail-system example is the mail client. We shall now
investigate it in more detail by using a debugger. The mail client has three methods: get-
NextMailItem , printNextMailItem , and sendMailItem . We will first investigate the
printNextMailItem method.
Before we start with the debugger, set up a scenario we can use to investigate (Exercise 3.35).
Exercise 3.35 Set up a scenario for investigation: Create a mail server, then create two mail
clients for the users "Sophie" and "Juan" (you should name the instances sophie and
juan as well so that you can better distinguish them on the object bench). Then use Sophie's
sendMailItem method to send a message to Juan. Do not read the message yet.
After the setup in Exercise 3.35, we have a situation where one mail item is stored on the server
for Juan, waiting to be picked up. We have seen that the printNextMailItem method picks
up this mail item and prints it to the terminal. Now we want to investigate exactly how this
works.
3.13.1
Setting breakpoints
To start our investigation, we set a breakpoint (Exercise 3.36). A breakpoint is a flag at-
tached to a line of source code that will stop the execution of a method at that point when it
is reached. It is represented in the BlueJ editor as a small stop sign (Figure 3.5).
You can set a breakpoint by opening the BlueJ editor, selecting the appropriate line (in our case,
the first line of the printNextMailItem method) and then selecting Set/Clear Breakpoint
from the Tools menu of the editor. Alternatively, you can also simply click into the area next to
the line of code where the breakpoint symbol appears, to set or clear breakpoints. Note that the
class has to be compiled to do this.
Exercise 3.36 Open the editor for the MailClient class and set a breakpoint at the first
line of the printNextMailItem method, as shown in Figure 3.5.
 
 
Search WWH ::




Custom Search