Strings are immutable. Once created, they cannot be changed. In your "s = s +" statements, you're allocating a new string with the result of the operation.
In the Try block, you allocated a string, "return value from try block". You then pushed the pointer to that string on the stack using the return statement.
Now, in your Finally block, you concatenate two strings together and then don't do anything with the resulting string. Remember, the pointer to the original string is what was pushed onto the stack for return to the caller, not a "value of s" or the pointer to the new string.
Now, if you try putting a return statement in the Finally block, you'll find that you can't.
The proper way to do this would be to NOT put the returns in the Try and Catch blocks, but after the try/catch/finally block entirely.
public static void main(String[] args)
{
System.out.println(methodReturningValue());
}
static String methodReturningValue()
{
string s = null;
try
{
s = "return value from try block";
}
catch (Exception e)
{
s = s + "return value from catch block";
}
finally
{
s = s + "return value from finally block";
}
return s;
}
Yes, I'm one of those people who subscribes to the "one return statement per method" paradigm.