To debug a Java program in NetBeans, you just select a project to debug or the class file itself, as shown in the following screenshot:
To debug line-by-line, you should add a breakpoint. So, you can place a breakpoint by clicking on the corresponding line number. Let's add a breakpoint in the beginning of the main method.
Use the following commands to debug line-by-line on the source code:
To inspect the value of a variable, just right-click on the code screen and select the New Watch option (or just press Ctrl + Shift + F7). Insert the name of the variable or expression you want to watch and click on Ok. You can see at the bottom of the screen a section called variables, where all the user custom expressions and relevant variables are displayed with their current values, as shown in the following screenshot:
In the preceding case, n
is an object, so you can expand all its attributes by clicking on the + sign on the left-hand side. And there you are; all the attributes are shown as follows: