Using Watchpoint (Intermediate)

Watchpoint is a very useful tool that is used to track access and/or modification of the variables. The Watchpoint is a type of breakpoint that stops whenever a variable is either accessed, modified, or both.

Getting ready

Please make sure that you don't have any breakpoints checked in the Breakpoints view list and the debugger is terminated.

How to do it...

  1. Go to the Outline view and find the number variable.
  2. Right-click on number and select Toggle Watchpoint. See the following screenshot:
    How to do it...

    If you go to line 9, you will see that there is a How to do it... Watchpoint icon instead of a breakpoint. This means that the current variable is being watched. Also, if you want to unset Watchpoint, just double-click on the icon. If you want to set it back, double-click again.

  3. Run the debugger.

    The first time when Watchpoint is triggered, the application stops at line 47; that is because the main() method at line 110 called the setNumber() method that assigned a number to the value set by the user.

  4. Click on the Resume icon.

    This time the application stops at line 90; that is because the toString() method requested access to the number variable. Note that number was modified the previous time, but this time number was accessed without modification.

  5. Terminate the debugger.

Now let's assume that we are not interested in watching the access of the variable, we just want to know where it is modified.

  1. Go to line 9, right-click on the How to do it... icon, and select Breakpoint Properties….
  2. Uncheck Access and click on Ok.

    Now your breakpoint icon looks like this: How to do it..., which means that Watchpoint will be triggered only when a variable is modified.

  3. Run the debugger.

    Once again the application stopped at line 47 because we are modifying the variable.

  4. Click on the Resume icon.

Now we can see the difference between this execution and the previous one. This time without stopping on line 90, the application just finished execution and terminated. This is because we are not tracking access of the variable, just its modification. The Watchpoint is a very useful tool if you want to track how your variable changes and when it gets accessed, but sometimes you want to simply run the application and just see the output in the console. For this purpose, you should use a conditional breakpoint and print its output to the user or simply use a breakpoint type called Printpoint.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.35.185