try-finally

Why does changing the returned variable in a finally block not change the return value?

允我心安 提交于 2019-11-26 00:39:42
I have a simple Java class as shown below: public class Test { private String s; public String foo() { try { s = "dev"; return s; } finally { s = "override variable s"; System.out.println("Entry in finally Block"); } } public static void main(String[] xyz) { Test obj = new Test(); System.out.println(obj.foo()); } } And the output of this code is this: Entry in finally Block dev Why is s not overridden in the finally block, yet control printed output? The try block completes with the execution of the return statement and the value of s at the time the return statement executes is the value