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

前端 未结 9 1378
离开以前
离开以前 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条回答
  •  旧时难觅i
    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.

提交回复
热议问题