Bug 57914 - Compiler settings should allow option for warning for use of uninitialized final variables
Summary: Compiler settings should allow option for warning for use of uninitialized fi...
Status: RESOLVED WONTFIX
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.0   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: 3.0 M9   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-04-08 11:17 EDT by Dave CLA
Modified: 2004-04-08 12:39 EDT (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Dave CLA 2004-04-08 11:17:44 EDT
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).
Comment 1 Olivier Thomann CLA 2004-04-08 11:39:53 EDT
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.
Comment 2 Philipe Mulet CLA 2004-04-08 12:39:23 EDT
No action planned, this would require extensive computations to figure some use 
of a variable is nested in a variable initializer expression.