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

Cannot access anonymous enclosing class of anonymous class

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 1.1.7
    • tools
    • generic
    • generic



      Name: tb29552 Date: 10/29/98


      /*
      Consider the following example
      (based on real code):

      The output when B.main() is run is "B".
      The expected output is "A".

      The B object returned by getA in main's A object
      cannot access its enclosing A instance. The
      expression "A.this" which should refer to the
      enclosing class according to the Inner Class
      Specification is interpreted as "this".

      */
      class A {
          public String toString() {
              return "A";
          }

          A getA() {
              return new A ();
          }

          void test() {
          }
      }

      class B extends A {
          public String toString() {
              return "B";
          }

          public static void main(String[] args) {
              new A () {
                  A getA() {
                      return new B () {
                          void test() {
                              System.out.println(A.this.toString());
                          }
                      };
                  }
              }.getA().test();
          }
      }

      (Review ID: 41518)
      ======================================================================

            tturnidgsunw Todd Turnidge (Inactive)
            tbell Tim Bell
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: