Why is a static class illegal in Java?

You can’t create a top level static class; that’s what the compiler is trying to tell you. Also have a look at the answer here as to why this is the case. The gist is:

What the static boils down to is that an instance of the class can
stand on its own. Or, the other way around: a non-static inner class
(= instance inner class) cannot exist without an instance of the outer
class. Since a top-level class does not have an outer class, it can’t
be anything but static.

Because all top-level classes are static, having the static keyword in
a top-level class definition is pointless.

Leave a Comment