Why the main program in Java is put into a class?

后端 未结 9 549
囚心锁ツ
囚心锁ツ 2020-12-28 17:37

Why does the main method have to be put into a class? I understand the main ideas of OOP but I cannot get why the main program is defined within a class. Will such a class i

9条回答
  •  难免孤独
    2020-12-28 18:23

    That's simply how Java was designed: (almost) everything is an object, and code can only exist as part of a class.

    Since the main() is static, it being called does not automatically lead to an instantiation of the class. However, it's perfectly possible (and quite common, at least in small Swing programs and Applets) to have the class that contains the main() be an otherwise normal class that is instantiated and used like any other class.

提交回复
热议问题