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?

1
  • Note that it isn't a keyword in C/C++ and C# either. Guess it's just a tradition. Dec 15, 2010 at 7:24

5 Answers 5

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.

4
  • 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, 2010 at 4:04
  • @Thilo: Correct. The method signature must be public static void main(String[] args) in order to be called at startup.
    – Asaph
    Dec 15, 2010 at 4: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, 2010 at 4:14
  • 1
    Also Nothing prevents you of having multiple Main in the same project it all depends on how you launch your project Dec 15, 2010 at 4: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.

1
  • All of your links are broken now.
    – hat
    Jan 8, 2020 at 7:13
0

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

2
  • 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? Dec 15, 2010 at 4: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. Dec 15, 2010 at 4:40
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?

0
-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
1
  • I'm not sure what you mean... but to me it does not seem to answer the question.
    – icedwater
    Feb 10, 2014 at 5:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.