Just started learning Java. Why is the main() inside of a class?

后端 未结 3 417
灰色年华
灰色年华 2021-01-14 09:17

I\'m learning Java and I noticed that the main() is put inside of a class. Why? I don\'t consider my main() to be a member of any object. So please

3条回答
  •  既然无缘
    2021-01-14 10:04

    You must put the main() in a class. And, it must be static (which means it is not a member of any Object). When you launch the Java Runtime Environment (JRE) it will load that class and invoke the main().

    This is covered by JLS-12.1 - Java Virtual Machine Startup which says in part,

    The Java Virtual Machine starts execution by invoking the method main of some specified class, passing it a single argument, which is an array of strings. In the examples in this specification, this first class is typically called Test.

提交回复
热议问题