Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6479551

Anonymous constructor with super() fails with wrong message

XMLWordPrintable

    • x86
    • windows_xp

      FULL PRODUCT VERSION :
      javac -version: javac 1.6.0-rc

      javac -J-version: java version "1.6.0-rc"
      Java(TM) SE Runtime Environment (build 1.6.0-rc-b97)
      Java HotSpot(TM) Client VM (build 1.6.0-rc-b97, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      The following code snippet:

      public class Test {
      public static void main(String[] args) {
      new Object(){
      {
      super();
      //above errors: "call to super must be first statement in constructor"
      }
      };
      }
      }

      generates an error on the 'super' call:
      "call to super must be first statement in constructor"

      The real bug is the text in the error message. It is misleading. The call to super was not in a constructor at all but in an initializer block.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      A. copy/paste snippet listed in 'description' to a 'Test.java' textfile.
      B. run "javac Test.java"


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I was expecting it to compile just fine, or possibly tell me that super() calls are not allowed inside a 'constructor' of an anonymous class.

      The error message received suggests a fix - to make it the first statement, but it already is.

        Suggested fix: Make it possible to call super() as first statement in constructors for anonymous classes.
      ACTUAL -
      C:\>"c:\Program Files\Java\jdk1.6.0\bin\javac.exe" Test.java
      Test.java:5: call to super must be first statement in constructor
                                      super();
                                           ^
      1 error

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      call to super must be first statement in constructor

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class Test {
      public static void main(String[] args) {
      new Object(){
      {
      super();
      //above errors: "call to super must be first statement in constructor"
      }
      };
      }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      No workaround available.

            Unassigned Unassigned
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: