The return value is evaluated and stored on the stack before the finally block is executed. Note that the value on the stack is actually the value of the StringBuilder reference. Thus, even if you set builder to null , this will not change the estimated return value already on the stack. However, if the return value is a reference to a mutable object, you can mutate the object, and the changes will be visible in the return value.
For example, if you add the statement below to the finally block instead of the nullifying link:
builder.append("Hrithik Roshan");
then you will see this content in the return value.
However, if you return builder again, from the finally block, it will be on top of the previous evaluated return . But remember, this is not a good idea.
source share