Why can't a top level class be static in Java?

前端 未结 8 1324
礼貌的吻别
礼貌的吻别 2020-12-04 07:30

Can\'t find a satisfactory answer anywhere.

相关标签:
8条回答
  • 2020-12-04 08:08

    static can be added nested classes of an interface, even though this is the default.

    I believe static cannot be added to top level classes because initially there were no nested classes and you couldn't add static to any class.

    Later nested class were added and static could be added to nested classes, however there is a tendency not to change the syntax any more than needed so it wasn't added to top level classes. (as there was no need/benefit)

    0 讨论(0)
  • 2020-12-04 08:09

    We can't declare outer (top level) class as static because the static keyword is meant for providing memory and executing logic without creating Objects, a class does not have a value logic directly, so the static keyword is not allowed for outer class.

    0 讨论(0)
提交回复
热议问题