0

Today I came across a scenario wherein an assignment was done inside a finally block, a similar code snippet as below

    public static void main(String[] abc) {
    System.out.println("Returned \t"+ testFinally());
}

public static int testFinally() {
    int aVariable =2;
    try {
        System.out.println("value of aVariable inside try \t"+aVariable);
        return aVariable;
    } finally {
        aVariable = 100;
        System.out.println("value of aVariable inside finally \t"+aVariable);
    }
}

The output was bit strange to me

value of aVariable inside try 2

value of aVariable inside finally 100

Returned 2

Then I tried the same with StringBuffer and String

With StringBuffer

    public static StringBuffer testFinallyStringBuffer() {
    StringBuffer strBuffer = new StringBuffer("ABCD");
    try {
        System.out.println("value of strBuffer inside try \t"+strBuffer);
        return strBuffer;
    } finally {
        strBuffer.append("EFGN");
        System.out.println("value of strBuffer inside finally \t"+strBuffer);
    }
}

Output

value of strBuffer inside try ABCD

value of strBuffer inside finally ABCDEFGN

Returned ABCDEFGN

Output was as expected.

With String

    public static String testFinallyString() {
    String str = "ABCD";
    try {
        System.out.println("value of str inside try \t"+str);
        return str;
    } finally {
        str = str + "EFGN";
        System.out.println("value of str inside finally \t"+str);
    }
}

Output

value of str inside try ABCD

value of str inside finally ABCDEFGN

Returned ABCD

From this behavior, it seems as if variable(primitive/immutable objects/mutable objects) was passed from try block to finally block like parameters are passed to a method. Although, use of finally block is to do clean-up activities.But I want to know if my understanding is correct or there is something more to it.

Update

Though, I could not found anything satisfactory from the Question Question with which this Question has been marked DUPLICATE with. However, there are few comments claiming that the evaluation of Return statement happens only once.

Having said that, Still variables are local scope. Any change made to them should be visible on accessing these later on.

Possibly, RETURN internally creates a copy of variable being returned(right before invocation of finally and actually returning to calling method) which is why primitive variable and immutable objects behaves different from mutable objects in this case.

Developer
  • 524
  • 1
  • 10
  • 20
  • 1
    @Luiggi I went through the post mentioned by you. But I did not get the answer I am looking for.Could you please point me to the answer addressing this question. – Developer May 08 '18 at 17:44

1 Answers1

0

There is nothing to do with the variable type passed into the finally block, but to do with you are returning a value or reference

In your 1st case, 2 is returned (as a copy) and the change to the variable inside the finally block is changing the local copy in the function

In your 2nd case, the same happens but since the return type is a reference, and you modify that in the finally block so the modification happens to the same underlying object

It is the same as if you pass in an int to a function vs pass in an object reference and change the object via the reference

Ken.C
  • 31
  • 1
  • Are you trying to say that finally block is executed after return statement execution ? – Developer May 08 '18 at 17:26
  • @Developer that's exactly the Q/A marked as duplicate for your question. – Luiggi Mendoza May 08 '18 at 17:34
  • @Developer, the return the function probably happen after the finally. But when you say "return x" , it probably save that value as a value to return, further change to that in the finally block wouldn't matter (from what you observe) – Ken.C May 09 '18 at 12:27