80

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 3

124

For a class or instance variable

  1. right-click on the variable in the outline view
  2. select "Toggle Watchpoint"
  3. Then, in the breapkoints view, you can right-click on the resulting entry
  4. select "breakpoint properties"
  5. deselect "Field Access".
Sign up to request clarification or add additional context in comments.

5 Comments

How to insert a breakpoint when any variable in the program equals a certain value?
@JudeDesir I'm pretty sure that feature doesn't exist anywhere, it would cause ridiculously bad performance.
Is there a similar method for local variables within methods?
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.
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.
23

OR Toggle Breakpoint on the line where the variable is declared, then right-click on the resulting entry, select "breakpoint properties" and deselect "Field Access".

Comments

-3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.