-1

I guess there's something about jvm.BUT how does jvm recognize it?For the name main,or the static property?

That's my little confusion.

  • Does this answer your question? [Why is the Java main method static?](https://stackoverflow.com/questions/146576/why-is-the-java-main-method-static) – tgdavies May 27 '22 at 02:26
  • That seems at most to partially answer the question, TBH. – Maarten Bodewes May 27 '22 at 02:37
  • Not really.I 'm clear about these modifier words.I'm just confused about how jvm work with their combination into a launchable entrance. – LittleRed1234 May 27 '22 at 02:53
  • 1
    The JVM doesn’t do anything about it. The execution starts with the `main` method, because the `java` command you’re using to start your program is searching for that method and calling it. – Holger May 30 '22 at 10:16

3 Answers3

1

When JVM runs an application by specifying a class, it will look for the main method with the signature of public static void main(String[]).

chenzhongpu
  • 6,193
  • 8
  • 41
  • 79
1

TLDR; Neither having a main method nor a static method is sufficient, and the method needs to be both, as specified by the Java Language Specifications (JLS).

If there is something about the JVM then it is usually specified in the Java Language Specifications. In this case it is section 12.1 which specifies the requirements for a main method:

public static void main(String[] args)

or

public static void main(String... args)

So instead of just being static it also needs to be public and have a void return type (integer makes more sense maybe, but Java is multithreaded, so the return value is given using System.exit(int)). Furthermore, it must accept String arguments, as you would expect.

Note that the main method has been designed to be used in a CLI environment such as the command line in Windows or one of the many shells in Linux / Unix / MacOS. It is similar to the C/C++ main method; Java was based on the C/C++ language.

I've also checked this against Java 18, and the text is still the same.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
1

Java main method is the entry point of any java program. Its syntax is always public static void main(String[] args).

When java runtime starts, there is no object of the class present. That’s why the main method has to be static so that JVM can load the class into memory and call the main method. If the main method won’t be static, JVM would not be able to call it because there is no object of the class is present

You can find a detailed explanation here.

Babad00k
  • 95
  • 1
  • 2
  • 7
  • but why is the name of main method fixed....i mean ,i'm literally clear about the public and static property.I just cant understand why is "main"?And if it's a tradition about C something,how does it work?I know nothing about C,sorry – LittleRed1234 May 27 '22 at 02:44
  • In Java "main" is not a keyword but JVM looks for the "main" method when it starts executing a Java program. The signature of the main method needs to be in a specific way for the JVM to recognize that method as its entry point. – Babad00k May 27 '22 at 02:52
  • Yes,thank you so much!That's the point.What specific way? – LittleRed1234 May 27 '22 at 02:57