开发者

How does the JVM use the "Main method" to start a Java program? [duplicate]

This question already has answers here: 开发者_如何转开发Entry point for Java applications: main(), init(), or run()? (5 answers) Closed 6 years ago.

main is not a keyword in java. How does the JVM know to execute the main method as the starting point of any program?


main doesn't need to be a keyword in java in order for the JVM to look for it at the start of execution. There is no conflict with other methods or variables also called main. This is simply how the JVM spec was designed. It was most likely borrowed from the c language.

Java Specification References: keywords, invoking main.


It's just specified in the JLS.

12 Execution

...

A Java virtual machine starts up by loading a specified class and then invoking the method main in this specified class. Section §12.1 outlines the loading, linking, and initialization steps involved in executing main, as an introduction to the concepts in this chapter. Further sections specify the details of loading (§12.2), linking (§12.3), and initialization (§12.4).

...

12.1 Virtual Machine Start-Up

A 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.

...

12.1.4 Invoke Test.main

Finally, after completion of the initialization for class Test (during which other consequential loading, linking, and initializing may have occurred), the method main of Test is invoked.

The method main must be declared public, static, and void. It must accept a single argument that is an array of strings.


It invokes this method by using reflection. You could take a look into Java Web Start sources for example


Basically every language was more or less similar to its predecessor. In C language it is the main() that executes at first but its not mandatory to happen all the time. Programs without main method can also be executed. So, Its just the convention by which the main method gets called at first.

please check:

Entry point for Java applications: main(), init(), or run()?

C program without a main function?


Main is basically a universal indicator of the central method so Java automatically makes it the central method when provided with public static

public static void main
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜