‘static’ value appears to reset after function call [duplicate]

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:

  1. Load the value of value onto the stack.
  2. Call the method foo and put the result onto the stack.
  3. Do subtraction with these two values on the stack.
  4. Set the result back to value field.

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)