Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

190
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!