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

Anonymous class can't be defined in interface field initializer

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Won't Fix
    • Icon: P4 P4
    • None
    • 1.2.0, 1.2.2
    • tools
    • generic
    • generic



      Name: igT44549 Date: 02/08/99


      My understanding is that the following program is legal:

      interface InterfaceField
      {
          Object o = new Object()
          {
          };
      }

      The anonymous class instance creation implicitly defines a static member class of the enclosing interface.

      Both Jikes and Visual J++ accept the above program, but javac rejects it.

      Please confirm.

      thanks.
      (Review ID: 53448)
      ======================================================================

      Name: tb29552 Date: 05/08/99


      /*
      Code will not compile. The error message is:

      % javac GetVals.java
      GetVals.java:18: The type nested type GetVals. 1 can't be static. Static members can only occur in interfaces and top-level classes.
        public static GetVals SAME = new GetVals() {
                                          ^
      GetVals.java:18: Interface fields can't be private or protected: nested type GetVals. 1
        public static GetVals SAME = new GetVals() {
                                          ^
      2 errors


      */
      public interface GetVals {

        public Object getVal(String arg) ;

        public static GetVals SAME = new GetVals() {
          public Object getVal(String s){
            return s;
          }
        } ;
      }
      /*
      -----------------------------------------------
      in fact GetVals.$1 IS INTENDED to be a static class
      this obliges us to do things such as :

      -------------------------------------------------
      public interface GetVls {

         public Object getVal(String arg) ;

         public static class getVls_1 implements GetVls{
            public Object getVal(String s){
               return s;
            }
         }
         public static GetVls SAME= new getVls_1() ;

      }
      ----------------------------------------------
      OR

      ---------------------------------------------
      public interface GetVals {
         public Object getVal(String arg) ;

         public static GetVals SAME = Vals.same() ;
      }

      --------
      public class Vals {
         public static GetVals same(){
            return new GetVals() {
            public Object getVal(String s){
               return s;}
            } ;
         }
      }


      ---------------------------------------------

      */

      (Review ID: 57993)
      ======================================================================

      Name: krT82822 Date: 09/20/99


      Apparently javac does not support anonymous classes nested directly inside interfaces.
      Sample source:
      ------------------------------------------->
      interface Foo {
          int bar = (new Object() {} ).hashCode();
      }
      <-------------------------------------------


      JDK 1.2.2 javac output on above:
      ------------------------------------------->
      Foo.java:2: The type nested type Foo. 1 can't be static. Static members can only occur in interfaces and top-level classes.
          int bar = (new Object() {} ).hashCode();
                         ^
      Foo.java:2: Interface fields can't be private or protected: nested type Foo. 1
          int bar = (new Object() {} ).hashCode();
                         ^
      2 errors
      <-------------------------------------------

      Jikes 1.02 and Pizza v0.39g successfuly compile the above source.

      Anonymous classes nested directly in interfaces are by definition top-level (i.e., not inner). As far as I can tell this is neither acknowledged nor forbidden by the nner Classes
      Specification. There doesn't seem to be any reason to disallow it.
      (Review ID: 95371)
      ======================================================================

            wmaddoxsunw William Maddox (Inactive)
            iris Iris Clark
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: