0

I understand why public static void main is used, I also know that String[] args creates a 1-D array called args which contains strings. But why must we have this with in the parenthesis?

MasterWali
  • 31
  • 1
  • 7

2 Answers2

2

The String[] args is to supply all the arguments that may be delivered to your program from the command line. Say for example you wanted a filepath as a parameter to your main, you can type it with the command line and it will pass that as the first element in the array. It allows you to pass nothing, or many things when running your main.

Zizouz212
  • 4,908
  • 5
  • 42
  • 66
0

The Java language was specified that the main method must take exactly one parameter of type String[]. It can be named any valid identifier you want; it's only a convention that it's named args. It can even be String.... Here's the specification, from the JLS, Section 12.1.4:

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)

It could even be

public static void main(String[] zzyzx)

but the parameter must be there.

Community
  • 1
  • 1
rgettman
  • 176,041
  • 30
  • 275
  • 357
  • 2
    He's asking about the parenthesis not the parameter. – dvallejo Jun 16 '15 at 17:00
  • @dvallejo The "this" in "But why must we have this with in the parenthesis?" refers to "a 1-D array called args which contains strings", the most recent thing referred to in the question, unless the OP would like to clarify, by editing the question to be more clear. – rgettman Jun 16 '15 at 17:04