Which access modifiers are legal on a top-level class, and why can't a top-level class be private or protected?
answer
- Top-level class: only public or package-private
- No private/protected on top-level classes
- private/protected need an enclosing type to be relative to
- Nested classes are members → all four modifiers allowed
- One public top-level type per file; file name must match
basics
~10 sA top-level class can only be public or have no modifier (package-private). private and protected are not allowed on top-level classes — those levels only make sense for members nested inside something.
solid answer
~50 sA top-level class — one declared directly in a file, not inside another class — may be either public (visible to all packages) or package-private (no keyword, visible only within its own package). It cannot be private or protected. The reason: private and protected are defined relative to an enclosing class. private means 'the enclosing class only', and protected means 'the package plus subclasses of the enclosing type' — both presuppose there's an outer scope to be relative to. A top-level class has no enclosing class, so those levels have no meaning and the compiler rejects them. Also, a public top-level class must live in a file whose name matches the class, and a file may hold at most one public top-level type. By contrast, nested classes (declared inside another class) are members and may use all four modifiers, exactly like fields and methods.
go deeper
State that a top-level class is either public or has no modifier, and that private/protected are not allowed there.
Explain WHY: private/protected are relative to an enclosing type that a top-level class lacks, and contrast with nested classes allowing all four.
Connect to file/compilation rules (one public type per file, name match) and to using package-private as the real tool for hiding implementation classes.
Relate top-level visibility to module export strategy (JPMS) and API surface design — package-private + non-exported packages as the encapsulation boundary between teams.
## Top-level vs nested A **top-level class** is a class declared at the outermost level of a source file — directly under the `package` declaration, not inside any other class. A **nested class** (member class) is declared *inside* another class, so it is a **member** of that class, like a field or method. ## What's legal on a top-level class Only two options: - **`public`** — visible from every package. - **default / package-private** (no keyword) — visible only within the same package. `private` and `protected` are **compile errors** on a top-level class. ```java public class A { } // legal class B { } // legal: package-private // private class C { } // COMPILE ERROR // protected class D { }// COMPILE ERROR ``` ## Why private/protected make no sense at top level Both `private` and `protected` are defined **relative to an enclosing type**: - `private` means "accessible only within the **enclosing class**." A top-level class has no enclosing class, so "private to what?" has no answer — there is no scope smaller than the package for it to hide in. - `protected` means "accessible within the package, plus to **subclasses of the enclosing type**." Again this is anchored to an enclosing type. For a top-level class with no encloser, the level is meaningless. The two levels that *do* make sense without an enclosing type are exactly "my package" (package-private) and "everyone" (public) — which is why those are the two the language permits. ## Nested classes are different Because a **nested** class is a member of its outer class, it *does* have an enclosing type, so all four modifiers apply to it — just like fields and methods: ```java public class Outer { private class Inner1 {} // only Outer protected class Inner2 {} // package + subclasses of Outer class Inner3 {} // package-private public class Inner4 {} // everyone } ``` ## Related file rules (worth knowing) - A source file may declare **at most one `public` top-level type**, and if it has one, the **file name must match** that class name (`A.java` for `public class A`). - You may put several **package-private** top-level classes in one file, but the idiomatic style is one top-level class per file. ## Why it matters Understanding this keeps you from reaching for `private`/`protected` on a top-level class to "hide" it. The correct tool to hide a top-level class from other packages is **package-private** (omit the keyword); to truly hide implementation classes you keep them package-private (or, in modular code, simply don't export their package).
- How do you hide a top-level helper class from other packages?Make it package-private (declare it with no access keyword) so only classes in the same package can use it.
- Can a nested class be private?Yes. A nested class is a member of its enclosing class, so it can use any of the four modifiers, including private.
saying these in an interview costs you the question
- Writing 'private class Foo' at file scope and expecting it to compile
- Believing a top-level class can be hidden with private — use package-private instead
- Confusing nested-class rules (all four allowed) with top-level rules (two allowed)
- Thinking a file can hold multiple public top-level classes