public static void main(String arg[ ] ) in java is it fixed?

前端 未结 9 1314
离开以前
离开以前 2020-12-03 03:25

I was recently asked in an exam if public static void main(String arg[]) format of main method was fixed? Can we change it? Can we use main

相关标签:
9条回答
  • 2020-12-03 03:58

    The signature of the main method is specified in the Java Language Specifications section 12.1.4 and clearly states:

    The method main must be declared public, static, and void. It must specify a formal parameter (§8.4.1) whose declared type is array of String.

    • it must be public otherwise it would not be possible to call it
    • it must be static since you have no way to instantiate an object before calling it
    • the list of String arguments is there to allow to pass parameters when executing a Java program from the command line. It would have been possible to define it without arguments but is more practical like that (and similar to other languages)
    • the return type is void since it does not make sense to have anything else: a Java program can terminate before reaching the end of the main method (e.g., by calling System.exit())

    The method signature can therefore be:

    public static void main( String[] args )
    public static void main( String... args )
    

    note that the varargs version (...) is only valid from Java 5

    As the Java language allows the brackets [] to be positioned after the type or the variable (the first is generally preferred),

    public static void main( String args[] ) // valid but usually non recommended
    

    is also valid

    0 讨论(0)
  • 2020-12-03 04:02
    • public-main() method must be used by any one of the outside the class as well as inside the class so its public

    • static-static is necessary bcoz in java if we define class than we define object for that class and than and only than we can use that class..but instead of this we directly use by write the word static

    • void-for main() cant return any value like int or char main()-main is the function or method which we can use for accessing the future of java String-in java all we write consider as a string args-arguments

    0 讨论(0)
  • 2020-12-03 04:08

    I cannot answer for the arguments of the method but it must be public because the jvm must be able to access the function and it must be static because the jvm does not know how to create an instance of your class.

    This post provides a good detailed answer about the reasoning for static: Why is the Java main method static?

    This post provides a good answer for why main is void: Why is main() in java void?

    0 讨论(0)
  • 2020-12-03 04:08

    If not, why is it not hard coded that main(String arg[]) would stand for public static void main(String arg[]) always?

    You can have methods called "main" with any signature and access you like. The special rules only apply to the method you want the JVM to call to start a program.

    public class Test {
      public static void main(String[] args) {
        StrangeMain m = new StrangeMain();
        System.out.println(m.main());
        System.out.println(m.main(new String[]{"aaa","bbb"}));
      }
    }
    
    class StrangeMain{
      protected String main(){
        return "xyzzy";
      }
      protected String main(String[] arg){
        return arg[0];
      }
    }
    

    compiles, runs, and outputs:

    xyzzy
    aaa
    
    0 讨论(0)
  • 2020-12-03 04:12

    If you look into JDK source code (jdk-src\j2se\src\share\bin\java.c):

    /* Get the application's main method */
    mainID = (*env)->GetStaticMethodID(env, mainClass, "main",
                       "([Ljava/lang/String;)V");
    ...
    {    /* Make sure the main method is public */
    ...
    mods = (*env)->CallIntMethod(env, obj, mid);
    if ((mods & 1) == 0) { /* if (!Modifier.isPublic(mods)) ... */
        message = "Main method not public.";
        messageDest = JNI_TRUE;
        goto leave;
    ...
    

    It becomes very clear that it must have only this signature.

    0 讨论(0)
  • 2020-12-03 04:15

    Public is a kind of access specifier due to which we can access it from outside the class. Since main is the function that acts as an execution point. Main function is called by the JVM which is outside the class so it must be declared as public.

    Static is a kind of keyword used in java to specify that there is no need to create any instance of it. As we know main method also reside inside a class and to access the particular function of a class from outside the class (In this case from JVM), an instance of that particular class is needed, so to avoid these we simply put static to make access main method outside of class.

    Void is the return type since other return type in main method is meaningless.

    String is a pre-defined class name in java. And args [] is a variable of array types. It’s a variable name of String type object. So we can also change the name of args []. String class and its object can be passed in a running program as an argument to pass information to the main method from command line.

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