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

tutorial incorrectly defines, discusses nested classes

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 6u9
    • 1.4.2
    • docs
    • b08
    • x86
    • windows_xp
    • Verified

      Name: rmT116609 Date: 04/22/2003


      DESCRIPTION OF THE PROBLEM :
      In the Java Tutorial, Trail: Learning the Java Language,
      Lesson: "Classes and Inheritance", section: "Implementing
      Nested Classes", can be read the following:

      "Definition: A nested class is a class that is a member of
      another class."

      This is not just an error in the definition. The ensuing
      discussion continues to equate nested classes with member
      classes.

      As per the JLS: member classes are those with class-body
      scope, i.e., declared directly in the class body, while a
      nested class is ANY class defined within another class.
      Seems like this topic gives many people trouble, so having
      the tutorial get it wrong is probably a bad thing.

      In fact, I came upon this bug because I was reading an exam
      prep book which has nested-member-inner all jumbled up, and
      I wondered if this book's error was being discussed on the
      web. My search turned up an impressive assortment of nested
      class taxonomies, nearly all of them at odds with the JLS,
      including, alas, the Java Tutorial.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1.Enter the current Java Tutorial
      2. Pick the trail: Learning the Java Language
      3. Pick the lesson: Classes and Inheritance
      4. Pick the section: Implementing Nested Classes

      http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html



      Please see:

      http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#44365

      The second paragraph states:

      /*
      A nested class is any class whose declaration occurs within the body of
      another class or interface. A top level class is a class that is not a
      nested class.
      */

      The sixth paragraph is also germain, but I would skip to section 8.5:
      http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#2460
      26

      The first paragraph:

      /*
      A member class is a class whose declaration is directly enclosed in another
      class or interface declaration. Similarly, a member interface is an
      interface whose declaration is directly enclosed in another class or
      interface declaration. The scope (?6.3) of a member class or interface is
      specified in ?8.1.5.
      */

      The crucial phrase is "directly enclosed". This is about scope. The two
      sections mentioned in the above paragraph lay this out:

      Section 8.1.5:
      http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#1898
      8

      The line after the grammar is:

      /*
      The scope of a declaration of a member m declared in or inherited by a class
      type C is the entire body of C, including any nested type declarations.
      */

      I would keep reading right into section 8.2, in particular:

      /*
      The members of a class type are all of the following:

      Members inherited from its direct superclass (?8.1.3), except in class
      Object, which has no direct superclass
      Members inherited from any direct superinterfaces (?8.1.4)
      Members declared in the body of the class (?8.1.5)
      */

      Section 6.3 is mentioned only to further nail down the scope of a member
      declaration - it is the source of 8.1.5's definition of member scope:
      http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#103228

      Boiling all this down into simple statements (hopefully an improvement over
      my previous attempt):

      The members of a class are those fields, methods, classes, and interfaces
      which have member scope. Member scope means visibility throughout the body
      of the class. A member class therefore is declared directly in the body of
      its enclosing class (not within an expression or block of any kind).
      Anonymous and local classes are not members.


      REPRODUCIBILITY :
      This bug can be reproduced always.
      (Review ID: 180561)
      ======================================================================

            sharonz Sharon Zakhour (Inactive)
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: