legal main method signature in java
Simply because that's the requirement of Java.
A main method/entry point to a program must be a method declared as public static void main(String[] args)
. Your method that was declared with a String
parameter was similar but not compatible.
An array is not the same as a single String - if someone invoked Java with three command-line parameters, the JVM would create a three-element string array, and then how would it pass this into your method that only takes a single string?
So in that case you were trying to launch a Java program based on a class that did not have an main method to act as an entry point.
(The reason why String...
works is because this is syntactic sugar for an array parameter, and compiles down to a method with the same signature.)
Finally, i found the answer of my question in Sun Certified Programmer for Java 6 book.
First question was, how many different legal ways of using main method?
Legal main method signatures are
public static void main(String a[])
public static void main(String[] a)
public static void main(String... a)
what does
(String... a)
means ??
To declare a method using a var-args
parameter, we need to follow with an ellipsis(...)
then use space and then name of the array that will hold the parameter received. So, above term known as Variable Argument and which means 0 to many.
And, rules of using variable argument parameters is, must be the last parameter in the method signature and can have only one var-args in a method.
Eg:
void myfunc(String... a) //legal
void myfunc(String a...) //illegal
void myfunc(String a,int... b) //legal
void myfunc(String... a,int b) //illegal
Its default in java. java compiler expects an array of command line arguments. thats why you need to specify string args[] or String...
public static void main(String... a) --> Is a method with a variable argument list, that is internally(in the method) treated as an array.
Legal Main Method Signature:
public static void main(String a[])
static public void main(String a[])
public static void main(String[] a)
static public void main(String[] a)
public static void main(String... a)
static public void main(String... a)