Why does a Java program require a “main()” method?

谁都会走 提交于 2019-12-31 02:59:11

问题


It is just a naming convention? Why can't any method be called while executing a program from a shell e.g

$> java myPackage.MyClass.myOwnEntryPoint(String[] str)

回答1:


Yes, that's a naming convention, inherited from C. The advantage is that this way, it's very simple to find out which method is supposed to be the main method just by looking at the code.




回答2:


The main method is the entry point that the java program for running Java applications (as opposed to applets or other things) looks for. As far as I'm aware, there's no way to tell java to look for a different method instead, so it's not just a naming convention; if you want your application to run (via the standard java tool, anyway), you want to give it a main method with the appropriate signature. (You can play games with static initializers, but that's another thing entirely.)

The name main is inherited from C, but it's not just a convention.



来源:https://stackoverflow.com/questions/5219306/why-does-a-java-program-require-a-main-method

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!