J.Olufsen
J.Olufsen

Reputation: 13915

Can outer class be defined as static and enclose inner static class?

It correct to define an outer class as static which have inside also a static class? Only one instance of outer and inner classes is needed. Can outer class be abstract and it's enclosed class be static?

Upvotes: 1

Views: 380

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500065

No, a top level class can't be static. The meaning of "static" in a class declaration is only relevant to nested classes. You can certainly have a static nested class within an abstract class though.

From the JLS section 8.1.1:

The modifier static pertains only to member classes (§8.5.1), not to top level or local or anonymous classes.

Note that if you want "only one instance" of a class, you should potentially make it a singleton - which is entirely separate, and not something which affects the class declaration itself.

Upvotes: 4

Related Questions