Java Reference
In-Depth Information
guarded by an if statement. Code 7.8 illustrates this approach. Note that this version assumes
that reportState either tests the debugging field itself or also calls the new printDebug-
ging method.
Code 7.8
A method for selectively
printing debugging
* A number button was pressed.
* @param number The number that was pressed.
public void numberPressed( int number)
printDebugging( "numberPressed called with: " + number);
displayValue = displayValue * 10 + number;
* Only print the debugging information if debugging
* is true.
* @param info The debugging information.
public void printDebugging(String info)
if (debugging) {
As you can see from these experiments, it takes some practice to find the best level of detail to print
out to be useful. In practice, print statements are often added to one or a small number of methods
at a time, when we have a rough idea in what area of our program an error might be hiding.
In Chapter 3, we introduced the use of a debugger to understand how an existing application
operates and how its objects interact. In a very similar manner, we can use the debugger to track
down errors.
The debugger is essentially a software tool that provides support for performing a walkthrough
on a segment of code. We typically set a breakpoint at the statement where we want to start our
walkthrough and then use the Step or Step Into functions to do the actual walking.
One advantage is that the debugger automatically takes care of keeping track of every object's
state, and, thus, doing this is quicker and less error prone than doing the same manually.
Search WWH ::

Custom Search