How can I easily detect when a variable changes value? I would like the execution of the program to break on the debugger whenever a specified variable changes value. Right now I'm using Eclipse's debugger.
3 Answers
For a class or instance variable
- right-click on the variable in the outline view
- select "Toggle Watchpoint"
- Then, in the breapkoints view, you can right-click on the resulting entry
- select "breakpoint properties"
- deselect "Field Access".
5 Comments
LJD
How to insert a breakpoint when any variable in the program equals a certain value?
Michael Borgwardt
@JudeDesir I'm pretty sure that feature doesn't exist anywhere, it would cause ridiculously bad performance.
Tim Foster
Is there a similar method for local variables within methods?
Michael Borgwardt
I'm not using eclipse these days, but I doubt that it exists. It's easy enough to just put a breakpoint on each line of the method that changes the variable.
Ocie Mitchell
This feature does exist, but enabling it can cause the program to run much more slowly. Use it sparingly. It is not always easy to see where a field might be changed, especially when the object containing it is passed to other methods.
I'm not sure about Eclipse, but in IntelliJ IDEA, you can right click on a break point and add the conditions, just like you would in an if statement. Then, the debugger only pauses at the break point if its condition is true.
For instance, in this case it only stops if min == 4.