Community
Participate
Working Groups
Run the code below, note that the value of the final variable i1 is printed differently on both invocations of foo. This may be an error in the VM spec (I've heard of some problems with finals), but ideally should have a compiler option in Eclipse to flag this as warning or error (maybe "Final variable i1 may not have been initialized"), since it may lead to difficult to find bugs (especially in a more complex code than the presented testcase). public class Test2 { final int i1 = foo(); final int i2 = foo(); private int foo() { System.out.println("foo " + i1); return 1; } public static void main(String[] args) { new Test2(); } } prints: foo 0 foo 1 Not sure whether it is even possible to detect this, since there might be a string of recursive/nested function invocations, and a function deep in the call stack may be the one using the initialized final (unfortunately, that's exactly the case when a bug like this may be very difficult to detect).
This is the normal behavior. When you initialize i1 the first time, you want the value of i1. In this case the default value is used and the default value is 0. This is then propagated back to complete the initialization of i1. When you invoke foo() the second time, i1 is initialized and its value is displayed. I would close as INVALID.
No action planned, this would require extensive computations to figure some use of a variable is nested in a variable initializer expression.