legal main method signature in java

前端 未结 8 1010
甜味超标
甜味超标 2020-12-01 21:44
class NewClass{
public static void main(String a){
    System.out.print(\"Hello\");
}
}

When I\'m trying to execute above code, then it shows an er

8条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-01 22:41

    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 
    

提交回复
热议问题