为什么更改finally块中的返回变量不会更改返回值? [英] Why does changing the returned variable in a finally block not change the return value?
问题描述
我有一个简单的Java类,如下所示:
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());
}
}
此代码的输出为:
Entry in finally Block
dev
为什么 s
未在 finally
块中覆盖,但控制打印输出?
Why is s
not overridden in the finally
block, yet control printed output?
推荐答案
尝试
块完成后执行<$ c在$ code> return 语句执行时,$ c> return 语句和 s
的值是方法返回的值。事实上 finally
子句后来改变了 s
的值(在返回之后
语句完成)不(在那时)更改返回值。
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 returned by the method. The fact that the finally
clause later changes the value of s
(after the return
statement completes) does not (at that point) change the return value.
请注意,上述内容涉及对<$ c $值的更改c> s 本身位于 finally
块中,而不是 s
引用的对象。如果 s
是对可变对象的引用( String
不是)和内容在 finally
块中被更改,然后这些更改将在返回的值中显示。
Note that the above deals with changes to the value of s
itself in the finally
block, not to the object that s
references. If s
was a reference to a mutable object (which String
is not) and the contents of the object were changed in the finally
block, then those changes would be seen in the returned value.
所有这些操作的详细规则可以在 Java语言规范的第14.20.2节。请注意, return
语句的执行将被视为 try
块的突然终止(以开头的部分)如果由于任何其他原因R .... 适用突然完成try块的执行。请参阅 JLS第14.17节为什么返回
语句是一个块的突然终止。
The detailed rules for how all this operates can be found in Section 14.20.2 of the Java Language Specification. Note that execution of a return
statement counts as an abrupt termination of the try
block (the section starting "If execution of the try block completes abruptly for any other reason R...." applies). See Section 14.17 of the JLS for why a return
statement is an abrupt termination of a block.
进一步详细说明:如果两者都有尝试
块和最后
阻止 try-finally的
语句由于
return
语句而突然终止,然后§14.20.2中的以下规则适用:
By way of further detail: if both the try
block and the finally
block
of a try-finally
statement terminate abruptly because of return
statements, then the following rules from §14.20.2 apply:
如果执行尝试
块因任何其他原因而突然完成R [除了抛出异常],那么最后执行
块,然后有一个选择:
If execution of the
try
block completes abruptly for any other reason R [besides throwing an exception], then thefinally
block is executed, and then there is a choice:
- 如果
最后
块正常完成,然后尝试
语句突然完成,原因是R。 - 如果
finally
块突然因为S而完成,然后尝试
语句完成abr由于理由S(以及理由R被丢弃)。
- If the
finally
block completes normally, then thetry
statement completes abruptly for reason R. - If the
finally
block completes abruptly for reason S, then thetry
statement completes abruptly for reason S (and reason R is discarded).
结果是 return
语句在 finally
块中确定整个 try-finally $ c的返回值$ c>语句,以及
try
块的返回值被丢弃。如果 try
块引发异常,则会在 try-catch-finally
语句中发生类似的事情,它会被一个 catch
块,以及 catch
块和最后
块有返回
语句。
The result is that the return
statement in the finally
block determines the return value of the entire try-finally
statement, and the returned value from the try
block is discarded. A similar thing occurs in a try-catch-finally
statement if the try
block throws an exception, it is caught by a catch
block, and both the catch
block and the finally
block have return
statements.
这篇关于为什么更改finally块中的返回变量不会更改返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!