Can I overload the main method?

can I overload the main method? If so, from which method is jvm launched?

+2
source share
7 answers

Yes. The main method can be overloaded just like any other method in Java.

Normal ad for main

public static void main(String[] args) throws Exception;

When you launch a Java application, it looks for a static method with the name " main", the return type " void'and the single argument of the string array. That is, what you throw does not matter when resolving this method.

Overloading provides several methods with the same name, but different arguments (and potentially return types).

With the above explanation, we can overload the main method.

+2
source

, JVM :

public static void main(String[] args);
+5

, .

public static void main(String[] args)

:

public class Test{

    public static void main(String [] args){
        System.out.println("First");
        main();
    }

    public static void main(){
        System.out.println("Second");   
    }
}

:

First
Second
+5

. , , , :

public static void main(String[] args)
+1

Java:

public, static void. , .

http://java.sun.com/docs/books/jls/third_edition/html/execution.html (12.1.4)

, public static void main(String[] args) .

0

String . , .

0

, . Jvm , , , , . , , "jvm", , ( ):

  • public, jvm ( ).
  • static, , ( ), , , jvm; " ( ) , , , . ".
  • void -, . .
  • main , .
  • String[] args , .
-1

Source: https://habr.com/ru/post/1746618/


All Articles