Java 如何检测变量何时更改值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/575336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:24:12  来源:igfitidea点击:

How to detect when a variable changes value

javaeclipsedebuggingvariables

提问by tatsuhirosatou

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.

如何轻松检测变量何时更改值?我希望程序的执行在指定变量更改值时在调试器上中断。现在我正在使用 Eclipse 的调试器。

采纳答案by Michael Borgwardt

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".
  1. 右键单击大纲视图中的变量
  2. 选择“切换观察点”
  3. 然后,在 breapkoints 视图中,您可以右键单击结果条目
  4. 选择“断点属性”
  5. 取消选择“现场访问”。

回答by andreyro

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".

或 在声明变量的行上切换断点,然后右键单击结果条目,选择“断点属性”并取消选择“字段访问”。

回答by sai

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 ifstatement. Then, the debugger only pauses at the break point if its condition is true.

我不确定 Eclipse,但在 IntelliJ IDEA 中,您可以右键单击断点并添加条件,就像在if语句中一样。然后,如果条件为真,调试器只会在断点处暂停。

For instance, in this caseit only stops if min == 4.

例如,在这种情况下,它仅在 时停止min == 4