'How can I modify a variable value while debugging in IntelliJ, so that respective watches are automatically updated?

I have the following piece of code:

Matcher matchDays = m_daysRegex.matcher(e.getKey());
if (matchDays.matches()){
...
}

Where e.getKey() is "Mon-Fri".

Now, I have already executed the first line and stand on the if expression. Using the watch window I evaluate the matchDays.matches() expression and then add some watches on the various methods of matchDays. Then using the same watch window I evaluate the matchDays = m_daysRegex.matcher("Mon,Fri") and matchDays.matches() expressions to see what happens. Unfortunately, the watch window does not refresh itself and it does not have an explicit button to do so: enter image description here

On the image above, notice how matchDays.group(0) displays "Mon,Fri", but matchDays.group(1) displays "Mon-Fri". This is because I manually refreshed the former, but all the rest still show the old values. I have to manually refresh them all, which is annoying.

Am I doing something wrong? Is there the right way to do it, so the watch expressions are refreshed automatically? Or is there a way to refresh the whole watch window?

Thanks.



Solution 1:[1]

To change the value of a variable in runtime in IntelliJ:

  1. Locate the variable in the Variables window.
  2. Right click and select "Set Value…".
  3. Update the value and then hit Enter.

Solution 2:[2]

It's not possible to refresh them automatically as a refresh would require calling the actual methods which can have side effects on the debugger and the semantics of the application. Simple watch values are refreshed automatically.

See also the similar request in YouTrack.

Solution 3:[3]

Adding to @Hari Rao

One small thing I think it is important to mention as it is easy to forget, make sure that the variable itself you're trying to modify is var and not val

Otherwise, you will get the "Set Value" greyed out.

Solution 4:[4]

The same in Android Studio. In debug mode go to Variables, select a variable and press Set Value.... If you don't see the variable, expand this list.

enter image description here

Then enter a new value and press Enter.

If you also add into Variables something like someVariable = 1 debugger will change someVariable value every time when it will reach a breakpoint.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pang
Solution 2 CrazyCoder
Solution 3 Richard Miller
Solution 4 CoolMind