[Solved] Why is the non-zero return int acting as 0? [closed]


I agree that it’s tricky. What teases in methodThree is that t is changed in the course of the call to methodTwo. So when you are looking at t.sum before and after that call, you are not looking at the same t. How is t changed? This happens when methodTwo calls methodOne, which in turn creates a new Test object and assigns it to t.

We also need to be aware of what the usual left-to-right evaluation means in this case. t.sum += methodTwo(); is in some sense evaluated left to right, in another sense right to left. What do I mean? Java starts from the left, looks at t.sum and from this decides which sum to add the method result to: the sum in the object that t refers to prior to the call. The call returns 90, which is added into the old t object. As I said, in the meantime a new t object has been created with a sum of 12. Therefore t.sum is also 12 after the statement has completed.

1

solved Why is the non-zero return int acting as 0? [closed]