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?
-
This question is better answered by a beginner Java book where they can cover this kind of question in more depth. – FreshWaterTaffy Jun 16 '15 at 16:55
-
http://stackoverflow.com/questions/890966/what-is-string-args-parameter-in-main-method-java – AzNjoE Jun 16 '15 at 16:58
2 Answers
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
.

- 4,908
- 5
- 42
- 66
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 declaredpublic
,static
, andvoid
. It must specify a formal parameter (§8.4.1) whose declared type is array ofString
. 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.
-
2
-
@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