I tried to execute the below method and i got the output "Inside try block" surprisingly. Could anyone explain why it is printing "Inside try block" instead of "Inside finally block"? Its like we are assigning new value to the strValue variable. However, still i am not able to understand why it is printing "Inside try block".
The return statement doesn't return the variable, it returns the value of the variable and the value of the variable when you called return was 'Inside try block'. Changing the value of the variable in the finally block doesn't affect the value that is returned.
Agreed. However, the string variable's value always replaces to new one when we assign new value to it if i am not wrong. The value or the strValue must be pointing to some memory address which contains the updated variable value. Right?
Rahul Nair wrote:Agreed. However, the string variable's value always replaces to new one when we assign new value to it if i am not wrong. The value or the strValue must be pointing to some memory address which contains the updated variable value. Right?
Yes. But as I said, the value of the variable at the time the return statement is called is what is returned. The return statement puts this value on the stack ready to be returned, before the finally block is called. At this point there is no connection between the variable and the value that is going to be returned.
Post by:autobot
Catch Ernie! Catch the egg! And catch this tiny ad too:
a bit of art, as a gift, the permaculture playing cards