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
How to detect when a variable changes value
提问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
对于类或实例变量
- 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".
- 右键单击大纲视图中的变量
- 选择“切换观察点”
- 然后,在 breapkoints 视图中,您可以右键单击结果条目
- 选择“断点属性”
- 取消选择“现场访问”。
回答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 if
statement. 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
。