Java Reference
In-Depth Information
4.
Set a breakpoint on that line and restart your application. When you get to that
line, step into it.
Alternate between stepping over and stepping into lines until you finally have
the defective code at its lowest level. Again, examine the values of any variables
related to the problem as you go.
By following that the preceding general flow, you can use a debugger to identify exactly where your
program did what you didn't want it to do, even though it didn't throw an exception.
5.
Debugging without a Debugger
In Java's early days (circa 1995), there was no debugger, either at the command line or in a nice program
such as Eclipse. Yet those of us using Java still had to find and fix our bugs. So, we fell back on the old
practice of writing values to the console. The System.out.println() method was our friend (though we
certainly got tired of writing it). We would add print statements to show the values of all the relevant
variables wherever we suspected a bug existed. In that fashion, we could inspect the values as the code
ran. It was a poor substitute for being able to step over and into lines with an actual debugger, but it
worked.
Programmers sometimes still use that technique, even when a debugger is available. For example, if
I think I can figure out a problem quickly with just a print statement or two, I'll try that first. If you use
print statements this way, remember to remove them when you solve the issue. I've been embarrassed
in more than one code review by a lingering print statement.
Fortunately for you, you get to learn Java at a time when mature tools have already been created,
so you don't have to rely on print statements. To that end, let's fire up the Eclipse debugger and see how
it works.
Starting the Eclipse Debugger
To start the Eclipse debugger:
1.
Open Eclipse.
2.
From the Window menu, choose Open Perspective, and then choose Debug.
The Debug perspective appears.
Tip After the first time you open the Debug perspective, a shortcut button appears on the right side of the
Eclipse toolbar. In the future, you can switch to the Debug perspective by clicking that button and back to the
regular perspective by clicking the Java button.
Let's start by opening the Firework class (the object that controls drawing a single firework). When
you first open the Firework class in the Debug perspective, you'll see a screen very similar to the one in
Figure 11-2.
Search WWH ::




Custom Search