【发布时间】:2012-11-09 13:06:35
【问题描述】:
为什么 Java 编译器在 finally 块中引发“局部变量 s 可能尚未初始化”。我不知道在哪个代码流中,s 仍未初始化。
public static void test() {
String s;
try {
s = "abc";
} catch (Throwable e) {
s = "throwable";
} finally {
System.out.println(s.getClass()); //---->(The local variable s may not have been initialized)
}
}
【问题讨论】:
-
因为局部变量还没有被初始化。
-
如果catch分支中的代码失败了怎么办
-
如果catch块中的赋值也失败了,那么它可能还没有被初始化
标签: java