Quick revision
The short answer
It prints "finally 1". When return x executes, the value of x, 1, is evaluated and saved before finally runs, so setting x to 2 in finally does not change what the method returns. If x referred to a mutable object, changes to the object's contents in finally would be visible to the caller, because only the reference was saved.
What to remember
In this answer, we saw that the code prints "finally 1". A return statement evaluates its expression and saves the value before finally runs, and javac keeps it in a hidden local slot, so reassigning x in finally does not change the result. For objects the saved value is a reference, so changes to the object's contents in finally reach the caller, while reassigning the variable does not. finally runs on every normal or abrupt completion of the try block, but not when the JVM exits or the process dies.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
It prints "finally 1". When return x executes, the value of x, 1, is evaluated and saved before finally runs, so setting x to 2 in finally does not change what the method returns. If x referred to a mutable object, changes to the object's contents in finally would be visible to the caller, because only the reference was saved.
Background to this question
This puzzle checks whether we know the exact order of events when a method returns through a finally block. It sounds academic, but it explains real behaviour: why a counter updated in finally does not affect a returned number, and why a list cleared in finally comes back empty. In this answer, we will trace the output, see where the return value is saved and why, compare a primitive with a mutable object, and recap when finally runs at all.
1Tracing the Output
Here is the code from the question. There are two print calls, and the order in which they happen decides the output.
To evaluate System.out.println(g()), Java must first call g() to get the argument. Inside g(), the return statement evaluates its expression, x, which is 1, and holds on to that value. The method cannot exit yet, because finally must run. finally sets x to 2 and prints "finally " without a newline. Then the method completes the pending return with the saved value, 1. Only now does println run, printing 1 and a newline. So the console shows finally 1.
2Where the Return Value Is Saved
The Java Language Specification says that a return statement with an expression first evaluates the expression and then attempts to transfer control to the caller, and that any finally blocks run during that transfer. The value is already computed by the time finally starts.
In bytecode we can see the mechanism. javac copies the value into a hidden local variable slot, runs the finally code, and then loads the value from that hidden slot to return it. The variable x and the hidden slot are two separate places, so changing x does not touch the saved copy.
- x = 1local variable
- return xevaluate x and save 1 in a hidden slot
- finally: x = 2changes x, not the saved value
- finally: print "finally "first output
- return the saved value
g()returns 1 - println(1)second output, so: finally 1
6 steps in order. Play them, or step through them one at a time.
So the answer to "which value is returned" is always: the value the return expression had when it was evaluated, unless finally itself executes another return, which would replace it.
3A Primitive Versus a Mutable Object
The saved value is whatever the expression produced. For an int, that is the number itself. For an object, it is the reference, the address of the object. The object is not copied, so changes made to the object in finally are visible to the caller.
Notice the two different effects in h(). append("b") changes the object that the saved reference points to, so the caller sees "ab". Assigning a new StringBuilder to sb only changes the local variable, just like x = 2 did, so the caller never sees "z". The second method shows how this turns into a bug: cleaning up a shared buffer in finally empties the result before the caller reads it.
4When Does finally Run at All?
finally runs whenever the try block completes, normally or abruptly: after a return, after an exception, after a break or continue. The exceptions are cases where the try block never completes. If code calls System.exit, the JVM shuts down without finishing the try statement, so finally does not run. The same is true if the process is killed by the operating system, for example with kill -9 or by a container that runs out of memory, or if the JVM crashes.
Older Java versions also had Thread.stop, which could abort a thread at any point; it is deprecated and no longer works in recent Java versions, so in modern code the realistic cases are JVM exit and process death. An infinite loop or a deadlock inside the try block also means finally never runs, simply because the block never finishes.
5Follow-up questions
What would g() return if finally also said return x?
It would return 2. A return in finally is an abrupt completion that replaces the pending return, and at that moment x is 2. The compiler warns about it with -Xlint:finally, because returning from finally also silently discards exceptions.
Does the same apply to a value computed by a method call?
Yes. In return compute(), compute() runs first and its result is saved, then finally runs. If finally changes some state that compute() used, the result is not recalculated.
Why does the output not show 1 before finally?
Because the value is printed by println in the caller, which can only run after g() has returned, and g() cannot return until finally has finished. The print inside finally happens while g() is still executing.
6Conclusion
In this answer, we saw that the code prints "finally 1". A return statement evaluates its expression and saves the value before finally runs, and javac keeps it in a hidden local slot, so reassigning x in finally does not change the result. For objects the saved value is a reference, so changes to the object's contents in finally reach the caller, while reassigning the variable does not. finally runs on every normal or abrupt completion of the try block, but not when the JVM exits or the process dies.
Open with
It prints finally 1. The println needs the result of g, so g runs first. Inside g, return x evaluates x, which is 1, and saves that value before finally runs.
Then, if they want more
Then finally sets x to 2, which only changes the local variable, and prints finally with a space. After that, the method returns the saved value, 1, and println prints it.
At the bytecode level, javac stores the return value in a hidden local slot, runs the finally code, then loads from that slot, so the variable and the returned value are separate. If x were a reference to a mutable object, like a StringBuilder or a list, the saved value would be the reference.
So appending to it or clearing it in finally would be visible to the caller, which is a real bug when someone clears a buffer in finally. Reassigning the variable to a new object would still not affect the result.
As for when finally runs: on every normal completion, return, exception, break or continue, but not if the JVM exits with System.exit, crashes, or the process is killed.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What does this print?": https://scrivio-interview-library.netlify.app/java/exceptions/130a5565c9/