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.

tatsuhirosatou
  • 25,149
  • 14
  • 39
  • 40

3 Answers3

125

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".
william.eyidi
  • 2,315
  • 4
  • 27
  • 39
Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
  • 2
    How to insert a breakpoint when any variable in the program equals a certain value? – LJD Apr 18 '19 at 22:47
  • 1
    @JudeDesir I'm pretty sure that feature doesn't exist anywhere, it would cause ridiculously bad performance. – Michael Borgwardt Apr 19 '19 at 19:55
  • Is there a similar method for local variables within methods? – Tim Foster May 23 '19 at 16:03
  • 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. – Michael Borgwardt May 24 '19 at 08:20
  • 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. – Ocie Mitchell May 26 '20 at 20:14
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".

andreyro
  • 935
  • 12
  • 18
-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.

sai
  • 1