This problem is not about static; it’s about how the subtraction works.
value -= foo(); can be expanded to value = value - foo()
The compiler will explain it into four steps:
- Load the value of
valueonto the stack. - Call the method
fooand put the result onto the stack. - Do subtraction with these two values on the stack.
- Set the result back to
valuefield.
So the original value of value field is already loaded. Whatever you change value in the method foo, the result of the subtraction won’t be affected.
If you change the order to value = - foo() + value, then the value of value field will be loaded after foo is called. The result is -8; that’s what you are expected to get.
Thanks for Eliahu’s comment.