legal main method signature in java

前端 未结 8 1011
甜味超标
甜味超标 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:15

    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.

    0 讨论(0)
  • 2020-12-01 22:24

    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)

    0 讨论(0)
  • 2020-12-01 22:28

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

    0 讨论(0)
  • 2020-12-01 22:32

    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

    0 讨论(0)
  • 2020-12-01 22:34

    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.

    0 讨论(0)
  • 2020-12-01 22:39

    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

    0 讨论(0)
提交回复
热议问题