Why can't we define a top level class as private?

A top-level class as private would be completely useless because nothing would have access to it.


Java doesn’t allow a top level class to be private. Only 'public' or 'package'.


I believe a better question would be:

What would it mean for a top level class to be private?

If you think in terms of access levels, the level above class is package. In fact you can have package private top level classes in Java! Taking from the Oracle (formerly Sun) Java tutorials:

If a class has no modifier (the default, also known as package-private), it is visible only within its own package (packages are named groups of related classes — you will learn about them in a later lesson.)

Depending on the answer to the question I asked, this might fit with your definition of a "top level private class".

Tags:

Java