-2

i just wonder why it is necessary to have String args[] in main method.

i know it is used for the command line argument, BUT everytime we are running our program, the command line is not necessary.

thanks in advance for answers.

6
  • 3
    No special reason other than that's the way it is specified by the Java Language Specification. Now, why the folks designed it like that, you can go on guessing as long as you want. Stephen C gives quite good arguments in the link posted by Nuno below my comment. Jan 11, 2013 at 13:40
  • 1
    Similar question: stackoverflow.com/questions/10783190/… Jan 11, 2013 at 13:40
  • 2
    Just a dumb question: if it was possible to have different main() method signatures as entry points, just how on earth would the JVM know which to run, if more than one were present?
    – ppeterka
    Jan 11, 2013 at 13:41
  • @ppeterka There could always be some embarassingly complex ruleset with frustrating edge cases :) Jan 11, 2013 at 13:43
  • 1
    @Marko why does "designed by committee" pop into my mind? :) Also, there should be a rule that different implementations must behave differently...
    – ppeterka
    Jan 11, 2013 at 13:45

2 Answers 2

5

This is specified in the Java specification.

The method main must be declared public, static, and void. It must specify a formal parameter (§8.4.1) whose declared type is array of String. Therefore, either of the following declarations is acceptable:

public static void main(String[] args)

public static void main(String... args)

If no arguments are given then the array is empty.

0

You use it for command line arguments as per Java specs. You can also use:

public static void main(String... args)

It is called the vararg notation and means that there can be zero or more arguments.

Not the answer you're looking for? Browse other questions tagged or ask your own question.