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

- 3,518
- 8
- 59
- 93
-
Note that it isn't a keyword in C/C++ and C# either. Guess it's just a tradition. – Sergei Tachenov Dec 15 '10 at 07:24
5 Answers
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.

- 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
-
1Also 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
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 executingmain
, 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 calledTest
....
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 methodmain
ofTest
is invoked.The method
main
must be declaredpublic
,static
, andvoid
. It must accept a single argument that is an array of strings.
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()?

- 1
- 1

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

- 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
-
2When 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
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

- 153
- 1
- 2
- 12
-
I'm not sure what you mean... but to me it does not seem to answer the question. – icedwater Feb 10 '14 at 05:52