我看到下面的
Java代码看起来不错,但从不编译:
public class UnwelcomeGuest { public static final long GUEST_USER_ID = -1; private static final long USER_ID; static { try { USER_ID = getUserIdFromEnvironment(); } catch (IdUnavailableException e) { USER_ID = GUEST_USER_ID; System.out.println("Logging in as guest"); } } private static long getUserIdFromEnvironment() throws IdUnavailableException { throw new IdUnavailableException(); // Simulate an error } public static void main(String[] args) { System.out.println("User ID: " + USER_ID); } }//Class ends here //User defined Exception class IdUnavailableException extends Exception { IdUnavailableException() { } }//Class ends here
以下是IDE中的错误信息:
变量USER_ID可能已经被分配.
静态最终变量的值赋值有问题吗?