当无法分配变量时,可能已经分配了变量

研究这段代码:

public class TestFinalAndCatch { private final int i; TestFinalAndCatch(String[] args) { try { i = method1(); } catch (IOException ex) { i = 0; // error: variable i might already have been assigned } } static int method1() throws IOException { return 1; } } 

编译器说java: variable i might already have been assignedjava: variable i might already have been assigned

但对我来说,这似乎是不可能的情况。

i是最终的,因此只能分配一次。 编译器可能不够智能,无法实现如果抛出exception,则不会发生第一个赋值,如果不抛出exception,则不会发生第二个赋值。

问题是在这种情况下编译器基于语法而不是基于语义的。 有2个解决方法:第一个基于移动exception句柄的方法:

 package com.java.se.stackoverflow; public class TestFinalAndCatch { private final int i; TestFinalAndCatch(String[] args) { i = method1(); } static int method1() { try { return 1; } catch (Exception ex) { return 0; } } } 

二,使用temporar变量:

 package com.java.se.stackoverflow; import java.io.IOException; public class TestFinalAndCatch { private final int i; TestFinalAndCatch(String[] args) { int tempI; try { tempI = method1(); } catch (IOException ex) { tempI = 0; } i = tempI; } static int method1() throws IOException { return 1; } } 

要解决此问题,请在try catch块中使用局部变量,然后将该结果分配给实例变量。

 public class TestFinalAndCatch { private final int i; TestFinalAndCatch(String[] args) { int tmp; try { tmp = method1(); } catch (IOException ex) { tmp = 0; } i = tmp; } static int method1() throws IOException { return 1; } } 

https://bugs.openjdk.java.net/browse/JDK-6326693?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel

看起来像JDK bug。 现在状态 – 固定。 但我无法找到哪个版本。