tags:

views:

130

answers:

3

Why does Java allow a top level class to be declared as private? Is there any other reason other than "We can't access a private class"?

+2  A: 

Well, you can't. A top-level class as privat would be completely useless because nothing would have access to it.

Maximilian Mayerl
+4  A: 

Java doesnt allow a top level class to be private. Only 'public' or 'package'.

Guillaume
A: 

In theory, you could instantiate and call methods on a private top-level class (if such a thing were allowed by the language ... which it isn't!), but you would have to use reflection to do this. Sensibly Sun decided that private top-level classes were not a sensible thing to support at the language level.

Actually, it is possible that the JVM might support top-level private "classes" created by bytecode magic. But it is not a useful thing to do.

Stephen C