This is maybe a dumb question, but I'm pretty surprised to see that using the private inner class as a generic type in the outer class isn't allowed.
If I make the inner class protected, it compiles fine.
Additionally, I have to precise Outer.Inner
instead of just Inner
, otherwise the inner class isn't found. This also looks a little weird.
Why can't Inner be private ? And why it is allowed to be protected ?
public class Outer extends AbstractSet<Outer.Inner> {
private static class Inner {
// ...
}
// ...
}
The error is:
Outer.java:3: error: Inner has private access in Outer
public class Outer extends AbstractSet<Outer.Inner> {
^
1 error
I'm using Java SE 17, but I think it doesn't matther much.
Note the meaning of private
from the language spec §6.6.1, emphasis mine:
Otherwise, the member or constructor is declared private. Access is permitted only when the access occurs from within the body of the top level class or interface that encloses the declaration of the member or constructor.
The extends
clause is not part of the class body. The syntax for a class declaration looks like this (Classes):
NormalClassDeclaration:
{ClassModifier} class TypeIdentifier [TypeParameters] [ClassExtends]
[ClassImplements] [ClassPermits] ClassBody
ClassBody:
{ {ClassBodyDeclaration} }
The "body" of the class refers strictly to the things inside the curly braces. Therefore, you cannot access the private class Inner
in the extends
clause.
If Inner
is protected
or package-private, on the other hand, then access is allowed, because both of them allows access from the same package.
Similarly, the scope of a member class declaration is the body of the class too (§6.3):
The scope of a declaration of a member m declared in or inherited by a class or interface C (§8.2, §9.2) is the entire body of C, including any nested class or interface declarations.
Therefore, the extends
clause is not in scope of the Inner
class, which is why you need to use a more qualified name to refer to it, rather than its simple name.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With