Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

191
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda