Roberto Mereghetti
Roberto Mereghetti

Reputation: 627

jls7 generic inner class documentation error

class Jls7error<T extends OutputStream> {
    class Jls7errorInner<S extends T> {
        public S out;
    }
}

According to jls7 Oracle documentation, this code should not compile:

It is a compile-time error to refer to a type parameter of a generic class C anywhere in
...
• any class nested within C.


http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.1.2
(pag 185, docs.oracle.com/javase/specs/jls/se7/jls7.pdf)
In fact, this code does compile and run on my jdk 1.7 Is it a documentation error?

EDIT: It was a documentation error in the PDF version. Oracle corrected the documentation in html and pdf documentation.

Upvotes: 1

Views: 75

Answers (2)

Dave Newton
Dave Newton

Reputation: 160271

(Moved to answer for space/formatting.)

The only place that references this states:

It is a compile-time error to refer to a type parameter of a generic class C anywhere in:

  • the declaration of a static member of C (§8.3.1.1, §8.4.3.2, §8.5.1), or
  • the declaration of a static member of any type declaration nested within C, or
  • a static initializer of C (§8.7), or
  • a static initializer of any class declaration nested within C.

Upvotes: 1

Jeffrey
Jeffrey

Reputation: 44808

I'm not sure where you're seeing any class nested within C. That section actually says

It is a compile-time error to refer to a type parameter of a generic class C anywhere in:

  • the declaration of a static member of C (§8.3.1.1, §8.4.3.2, §8.5.1), or
  • the declaration of a static member of any type declaration nested within C, or
  • a static initializer of C (§8.7), or
  • a static initializer of any class declaration nested within C.

Here's an example to demonstrate what each bullet is disallowing:

public class Foo<T> {
    private static T t; // first bullet makes this a compiler error

    static {
        T t; // third bullet makes this a compiler error
    }

    private static class Bar {
        private static T t; // second bullet makes this a compiler error

        static {
            T t; // fourth bullet makes this a compiler error
        }
    }

    private class Baz {
        private static T t; // second bullet again

        // you can't have a static initializer
        // in a non-static nested class
    }
}

Upvotes: 3

Related Questions