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

后端 未结 3 396
灰色年华
灰色年华 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 09:52

    By nature, Java is highly object oriented. So everything must be encapsulated within a class. All methods must be placed inside of a class. However, the main() is different. There can only be one main function in a class and it must always be static, meaning it is not part of an object and there is only one instance of it. When a java application is executed, the JRE will look for the main class (i.e. the class containing the main function). main() is where the execution starts. But due to the very nature of OO, it must be placed in a class. You can say that this is simply because of the skeletal structure of java. No other reason in particular.

提交回复
热议问题