例题1
public
class
TestDemo {
static
String
s
=
"A"
;
public
static
void
main(String[] args) {
System.
out
.println(test());
System.
out
.println(
s
);
}
private
static
String test() {
try
{
System.
out
.println(
"A"
);
return
s
=
"A"
;
}
finally
{
System.
out
.println(
"B"
);
s
=
"B"
;
}
}
}
输出:ABAB
总结:
finally 块的语句在 try 或 catch 中的 return 语句执行之后 返回之前执行,且 finally 里的修改语句可能影响也可能不影响 try 或 catch 中 return 已经确定的返回值,若 finally 里也有 return 语句则覆盖 try 或 catch 中的 return 语句直接返回。