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

Instantiation of local, inner class fails because of scoping problem.

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 1.1.4
    • tools
    • generic
    • generic


      allan.jacobs@Eng 1997-10-19

      A local, inner class cannot be instantiated inside a block in the
      same method in which the inner class is declared.

      algol% which java
      /usr/java/bin/java
      algol% java -fullversion
      java full version "1.1.4n:1997.10.16"
      algol% setenv CLASSPATH .
      algol% cat X.java

      class X {
              int a;
              X() {
                      a = 1;
              }
              void func() {
                      X ox ;
                      { // Block
                              ox = new X();
                              System.out.println("ox.a="+ox.a+" (expect 1)");
                      }
                      { // Block
                              class Y { // Local class declaration
                                      public int x;
                                      {
                                              x = 4;
                                      }
                              }
                              { // Block
                                      Y oy = new Y();
                                      System.out.println("oy.x="+oy.x+" (expect 4)");
                              }
                      }
              }
              public static void main(String[] argv)
              {
                      new X(). func();
                      System.exit(0);
              }
      }
      algol% javac -d . X.java
      X.java:21: Class Y not found in type declaration.
                                      Y oy = new Y();
                                      ^
      X.java:21: Class Y not found in type declaration.
                                      Y oy = new Y();
                                                 ^
      2 errors

            wmaddoxsunw William Maddox (Inactive)
            ajacobssunw Allan Jacobs (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: