为什么更改 finally 块中的返回变量不会更改返回值?
2022-08-31 07:42:02
我有一个简单的Java类,如下所示:
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());
}
}
这段代码的输出是这样的:
Entry in finally Block
dev
为什么在块中没有被覆盖,但控制打印输出?s
finally