Try this piece of code. Why does getValueB() return 1 instead of 2? After all, the increment() function is getting called twice.
public class ReturningF
See my comment.
It would return 2 if you had finally { return increment(); }.
The first return statement's expression is evaluated prior to the finally block. See Section §14.20.2 of the JLS.
If execution of the
tryblock completes normally, then thefinallyblock is executed, and then there is a choice:
- If the
finallyblock completes normally, then thetrystatement completes normally.- If the
finallyblock completes abruptly for reasonS, then thetrystatement completes abruptly for reasonS.
Calling getValue2 (as you have it now) twice would result in 1 followed by 3.