legal main method signature in java

烂漫一生 提交于 2019-12-17 09:55:13

问题


class NewClass{
public static void main(String a){
    System.out.print("Hello");
}
}

When I'm trying to execute above code, then it shows an error, main method not found. But when I changed public static void main(String a) to public static void main(String... a) or public static void main(String a[]). Then, it works..!!

So, My question is how many different ways we can write legal main method signature and what this signature public static void main(String... a) means ?


回答1:


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.)




回答2:


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 



回答3:


Its default in java. java compiler expects an array of command line arguments. thats why you need to specify string args[] or String...




回答4:


All these are valid/legal declarations of the main function in Java.

public static void main(String[] args) {
    // code
}

static public void main(String[] args) {
    // code
}

static public void main(String args[]) {
    // code
}

public static void main(String[] MarkElliotZuckerberg) {
    // code
}

public static void main(String... NewYork) {
    // code
}

The points to remember is -

  • The keywords public and static are interchangeable but mandatory.
  • The parameter of the main method can take the var-args syntax.
  • The name can be anything..!

Just for practice.. :P ...These are examples of invalid main method declarations -

static void main(String[] args) {
    // public is missing
}

public void main(String args[]) {
    // static is missing
}

public static int main(String... Java) {
    // return type not void

    return 0;
}

public void Main(String args[]) {
    // "main" not "Main"
}

public void main(string args[]) {
    // "String" not "string"
}

public void main(String.. SayHi) {
    // Ellipses is 3 dots !
}

Some give errors, while others simply overload the main method... Hope this helps...! If it did, let me know by commenting..!

Source - Java Tutorials on Theory of Programming




回答5:


public static void main(String a[]) is the main entry point signature for a typical Java program. So you should get on with this method signature.




回答6:


Java Runtime tries to find a method with name "main" with argument types "String[]". It is just like using reflection for finding a method with type arguments as String[].

Also String[] are used so that the runtime can pass the program arguments or Command Line arguments that are provided. It is like the runtime tokenizes the arguments with white space characters and then calls this method named "main".

The method main must be declared public, static, and void. It must accept a single argument that is an array of strings. This method can be declared as either

public static void main(String[] args)
or

public static void main(String... args)

You can also refer to Oracle Java specification documentation for more understanding

Chapter 12 Execution - Section 12.1.4 Invoke Test.main




回答7:


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)




回答8:


You need exactly the String[] args parameter (it's an array).

The reason is that you need to declare the main() method with a specified signature (and a method signature contains its name, number of its parameters and the types of the parameters).

So if you create a function which a different parameter type (String vs. String array), it is not recognized.



来源:https://stackoverflow.com/questions/13603445/legal-main-method-signature-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!