Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

192
Visualizações
Why using inner class in outer class generic type parameter not allowed if inner class is private?

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.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda