Bloodnock
Bloodnock

Reputation: 16

Java main method

In a overloaded main method why the main method with signature String[] args is considered as the entry point.

e.g.

public class Test {
    public static void main(String[] args) {
        System.out.println("why this is being printed");
    }

    public static void main(String arg1) {
        System.out.println("why is this not being printed");
    }

    public static void main(String arg1, String arg2) {
        System.out.println("why is this not being printed"); 
    }
}

Upvotes: 0

Views: 1472

Answers (2)

Witold Kaczurba
Witold Kaczurba

Reputation: 10485

This is the way Java works and Java documentation describes that;

Signatures other than specified will simply not work as they do not comply with the standard.

The java command starts a Java application. It does this by starting the Java Runtime Environment (JRE), loading the specified class, and calling that class's main() method. The method must be declared public and static, it must not return any value, and it must accept a String array as a parameter. The method declaration has the following form:

public static void main(String[] args)

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/java.html

Upvotes: 0

jrtapsell
jrtapsell

Reputation: 7001

The main method should have only 1 argument, of type String[] so the single string and 2 string forms are not valid main methods, and as such are not options, the only accepted forms are:

  • public static void main (String[])
  • public static void main (String...)

The second option is syntactic sugar for the first option.

This is set in the Java Language Specifications:

12.1. Java Virtual Machine Startup

The 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...

Link

Upvotes: 2

Related Questions