When creating a class with a generic type, it seems to be impossible to use a private class as type parameter, even when the class is an inner class of the generic type. Consider this code:
import java.util.Iterator;
import test.Test.Type;
public class Test implements Iterable<Type> {
@Override
public Iterator<Type> iterator() {return null;}
static class Type {}
}
The example above compiles, whereas the same example does not compile when Type
is private:
import java.util.Iterator;
// ERROR: The type test.Test.Type is not visible
import test.Test.Type;
// ERROR: Type cannot be resolved to a type
public class Test implements Iterable<Type> {
@Override
// ERROR: The return type is incompatible with Iterable<Type>.iterator()
public Iterator<Type> iterator() {return null;}
private static class Type {}
}
Why is it impossible to use the private class as a type argument for its enclosing class? Even though being private, in my opinion the class Type
should be visible within the class Test
.