Bug 343480 - [compiler] Incorrect/confusing error message on inner class static field declaration
Summary: [compiler] Incorrect/confusing error message on inner class static field decl...
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.7   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.8 M3   Edit
Assignee: Ayushman Jain CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-04-21 02:50 EDT by Deepak Azad CLA
Modified: 2012-02-16 01:28 EST (History)
4 users (show)

See Also:
srikanth_sankaran: review+


Attachments
proposed fix (4.22 KB, patch)
2011-09-29 06:10 EDT, Ayushman Jain CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Deepak Azad CLA 2011-04-21 02:50:22 EDT
---------------------------------------------------------------
public class A {
    public class B {
        static final int X = 1;
        static final Integer Y = new Integer(2);
        static final String s = "sdf";
    }
}
---------------------------------------------------------------

There is one error here, with the message "The field Y cannot be declared static; static fields can only be declared in static or top level types", which is wrong.
Comment 1 Ayushman Jain CLA 2011-04-21 04:42:15 EDT
Note that javac 1.7 warns 
"modifier 'static' is only allowed in constant variable declarations".

This will need a new error message, hence deferring to 3.8
Comment 2 Olivier Thomann CLA 2011-04-21 08:16:27 EDT
Static fields initialized with a constant expressions are fine.  The error message should reflect that.
Comment 3 Ayushman Jain CLA 2011-09-29 06:10:43 EDT
Created attachment 204279 [details]
proposed fix

Corrects the error message to say "The field {0} cannot be declared static in a non-static inner type, unless initialized with a constant expression". Updated tests
Comment 4 Ayushman Jain CLA 2011-09-29 06:11:18 EDT
Srikanth, does the new diagnostic look ok?
Comment 5 Srikanth Sankaran CLA 2011-10-18 01:51:49 EDT
(In reply to comment #4)
> Srikanth, does the new diagnostic look ok?

Apologize for the delay, Ayush. Agree that the modified diagnostic is
clearer and consistent with javac. For the record, javac also seems 
have to revised the message at JDK7 timeframe.
Comment 6 Ayushman Jain CLA 2011-10-19 02:55:37 EDT
Released in HEAD for 3.8M3 via commit 3b4463c1006c09cac09c3007a035ccb5f09314da
Comment 7 Srikanth Sankaran CLA 2011-10-24 02:16:53 EDT
Verified for 3.8 M3 using build id: N20111022-2000.
Raised Bug 361771 as follow up.
Comment 8 Srikanth Sankaran CLA 2011-10-24 03:52:23 EDT
(In reply to comment #7)
> Verified for 3.8 M3 using build id: N20111022-2000.
> Raised Bug 361771 as follow up.

That turned out to be a dup of bug 343420
Comment 9 Srikanth Sankaran CLA 2012-02-15 19:40:50 EST
Now I see an issue with:

public class X {
    public static void main(String[] args) {
        class Local {
            private static final long CONSTANT = 1L; // code select fails
            static int x = 10;
        }
        new X() {
            private static final long FINAL = 1L; // code select fails
        };
    }
}

we complain: 

The field x cannot be declared static in a non-static inner type, unless initialized with a constant.

It is being with a constant after all. We could do:

The field x cannot be declared static in a non-static inner type, 
unless also final ?
Comment 10 Ayushman Jain CLA 2012-02-15 23:57:47 EST
(In reply to comment #9)
> The field x cannot be declared static in a non-static inner type, 
> unless also final ?
But this would sound ambiguous for the error case shown in comment 0, where all fields are static and final

javac says "modifier 'static' is only allowed in constant variable declarations". This sounds more generic for both of the above cases. So maybe we should also fine-tune the message to say "The field {0} cannot be declared static in a
non-static inner type, unless it is final and initialized with a constant expression". Too wordy?
Comment 11 Srikanth Sankaran CLA 2012-02-16 01:28:58 EST
(In reply to comment #10)
> (In reply to comment #9)
> > The field x cannot be declared static in a non-static inner type, 
> > unless also final ?
> But this would sound ambiguous for the error case shown in comment 0, where all
> fields are static and final

Mmmh. 
> 
> javac says "modifier 'static' is only allowed in constant variable
> declarations".

Isn't "constant variable" oxymoronic ? :)

How about "The field x cannot be declared static in a non-static inner type
as it is not a constant"