12

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

Sanjay Jain
  • 3,518
  • 8
  • 59
  • 93

5 Answers5

12

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.

Asaph
  • 159,146
  • 25
  • 197
  • 199
  • 2
    +1 Note that it is not only the method name that matters, it also needs to be `static` (maybe `void`, too) and take a `String[]` as its only arguments. – Thilo Dec 15 '10 at 04:04
  • @Thilo: Correct. The method signature must be `public static void main(String[] args)` in order to be called at startup. – Asaph Dec 15 '10 at 04:09
  • Back in the old Mac OS 7 (or 8?) days, where there was no command line, the Mac JVM would also allow a main without args. Thankfully, this has been unified now (does not work anymore). – Thilo Dec 15 '10 at 04:14
  • 1
    Also Nothing prevents you of having multiple Main in the same project it all depends on how you launch your project – Jason Rogers Dec 15 '10 at 04:26
10

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.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

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?

Community
  • 1
  • 1
0

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

Petro Semeniuk
  • 6,970
  • 10
  • 42
  • 65
  • It invokes this method by using reflection. So this mean at run time it search for the class which has main method. is it right? – Sanjay Jain Dec 15 '10 at 04:07
  • 2
    When running jar file you specifying class name with main method, or, you could specify main class in within manifest in jar. In Java Web Start you specifying main class in jnlp file. – Petro Semeniuk Dec 15 '10 at 04:40
-2

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
DSdavidDS
  • 153
  • 1
  • 2
  • 12