In this recipe we will learn why breakpoints are important, how to set them up, and how to navigate through the code using the Step Into, Step Over, and Step Return breakpoint manipulation options.
Let's practice setting up a breakpoint on our sample program.
I am sure you are aware that in Java, the first method that is executed is the main()
method. Our Employee
class has its own main()
method, so let's set up a breakpoint in it.
If your project has more than one class, and the main()
method is located in a different class from the one you are about to debug, go to the icon and select Debug | Debug Configurations…. Select the class you are about to debug in the left menu and set the Main class option to the class that has the main()
method.
To set up a breakpoint
main()
method of the Employee
class.andrew.setNumber(123)
(on line 110).After running the debugger you should see that your program has highlighted the line with the breakpoint, as shown in the following screenshot:
This means that the program has stopped executing before line 110 and is waiting for your actions.
Before we proceed with exploring how Step Into/ Step Over works, let's take a look at the Variables view. Right now there are two lines: args that represents arguments passed to the main()
method and andrew. Expand andrew and you will see the current state of all the class variables as shown in the following screenshot:
We will keep on watching this view as it will change when we go through our class.
Let's learn how to navigate through the debugger.
We navigate with the help of the Step Into/Step Over buttons. Let's see how it works.
At this point we are standing at line 110 and there are four options that we can choose. We can Step Into, Step Over, Go to Next breakpoint, or Go Back.
Step Into means that we go inside the method and explore its functionality. Let's try it right now. In the top menu there are two buttons: . The first button is responsible for Step Into (F5) and the second one for Step Over (F6).
Now you are taken to the setNumber()
method. If you take a look at the Variables view, there are two lines now: this
, which relates to the class variables, and _number
, which is a variable of the local method.
Your position is at the if
statement evaluating if the number is between certain limits (at line 46).
As there is no method to Step Into, the pointer moves one line down; now it's on number = _number
.
This line assigns local variables to the class variable.
Right now the pointer should be at line 49.
Now let's look at the Variables view again. If you expand this, you will see that the number is highlighted in yellow. This is because the variables have changed their values. See the following screenshot:
This highlighting is very useful, as it allows you to see what variables have changed their values right after it has happened.
Before we use Step Into one last time in this exercise, let's also take a look at the Debug view. Sometimes, when the project is big, it is very hard to determine what is the hierarchical structure of the classes and methods, which you are in at the moment. In our case the Debug view shows that we are in the setNumber(int)
method on line 49, and if we look below this line, we see that we came here from the main()
method on line 110. See the following screenshot:
As we have reached the end of the setNumber()
method, Step Into makes it return back to the main()
method, and the pointer points to the next line (line 111 in our case). Also, the Debug view no longer shows setNumber()
in its hierarchy.
Now, let's consider the situation when you don't want to step into the method but you want to go directly to the next method. In this situation we use Step Over (F6). Note that when you use Step Over, the method is still called but the debugger does not walk you through it.
Let's step over the setAge()
method.
Clicking on Step Over (F6), you might see that you were not prompted inside the setAge()
method, but directly to line 112. Take a look at the Variables view. You will see that age
is set to 28
(that is what the setAge()
method does), despite you not walking through this method.
The last two controls that I want to mention in this recipe are Resume (F8) , which allows you to continue running your application until it next faces the breakpoint or the end of the program, and Terminate (Ctrl + F2), which stops the debugging process.
3.135.216.75