Why do we need the main () method in the main java class

I know that we can compile and run the java program successfully without the main () method, but why do we still need the main () method in the main java class?

+5
source share
5 answers

Each Java application should contain a main method whose signature looks like this:

   public static void main(String[] args)

How the main method is called

The main Java method is similar to the main function in C and C ++. When the Java interpreter runs the application (by calling it in the application control class), it starts by calling the main method of the class. Then the main method calls all the other methods needed to launch your application.

Java , , , :

 In class NoMain: void main(String argv[]) is not defined

, : String.

   public static void main(String[] args)

, . . , . , , :

    -descending

http://journals.ecs.soton.ac.uk/java/tutorial/getStarted/application/main.html

+7

, -. - main.

+6

Java- main, main() *.

main - , . .

* , , ,

+5

Java (JLS) "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"

,

public static void main(String[] args)

public -

static - ( JVM , , , ).

void - void.

Thus, the main () method is "hard-coded" in the JVM to say that its starting point.

+3
source

main () is the starting point of the application. When the application starts, this function is what is first evaluated from your code. He is responsible for launching your application.

+2
source

All Articles