0

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"); 
    }
}
1
  • 1
    Because that's the just way Java works. Commented Sep 12, 2017 at 1:29

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.