Why is the Java main method static?

2018-12-31 04:56发布

The method signature of a Java main() method is:

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

Is there a reason for this method to be static?

标签: java static main
30条回答
梦醉为红颜
2楼-- · 2018-12-31 05:26

If the main method would not be static, you would need to create an object of your main class from outside the program. How would you want to do that?

查看更多
呛了眼睛熬了心
3楼-- · 2018-12-31 05:29

Why public static void main(String[] args) ?

This is how Java Language is designed and Java Virtual Machine is designed and written.

Oracle Java Language Specification

Check out Chapter 12 Execution - Section 12.1.4 Invoke Test.main:

Finally, after completion of the initialization for class Test (during which other consequential loading, linking, and initializing may have occurred), the method main of Test is invoked.

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)

Oracle Java Virtual Machine Specification

Check out Chapter 2 Java Programming Language Concepts - Section 2.17 Execution:

The Java virtual machine starts execution by invoking the method main of some specified class and passing it a single argument, which is an array of strings. This causes the specified class to be loaded (§2.17.2), linked (§2.17.3) to other types that it uses, and initialized (§2.17.4). The method main must be declared public, static, and void.

Oracle OpenJDK Source

Download and extract the source jar and see how JVM is written, check out ../launcher/java.c, which contains native C code behind command java [-options] class [args...]:

/*
 * Get the application's main class.
 * ... ...
 */
if (jarfile != 0) {
    mainClassName = GetMainClassName(env, jarfile);

... ...

    mainClass = LoadClass(env, classname);
    if(mainClass == NULL) { /* exception occured */

... ...

/* Get the application's main method */
mainID = (*env)->GetStaticMethodID(env, mainClass, "main",
                                   "([Ljava/lang/String;)V");

... ...

{    /* Make sure the main method is public */
    jint mods;
    jmethodID mid;
    jobject obj = (*env)->ToReflectedMethod(env, mainClass,
                                            mainID, JNI_TRUE);

... ...

/* Build argument array */
mainArgs = NewPlatformStringArray(env, argv, argc);
if (mainArgs == NULL) {
    ReportExceptionDescription(env);
    goto leave;
}

/* Invoke main method. */
(*env)->CallStaticVoidMethod(env, mainClass, mainID, mainArgs);

... ...
查看更多
萌妹纸的霸气范
4楼-- · 2018-12-31 05:29

The public static void keywords mean the Java virtual machine (JVM) interpreter can call the program's main method to start the program (public) without creating an instance of the class (static), and the program does not return data to the Java VM interpreter (void) when it ends.

Source: Essentials, Part 1, Lesson 2: Building Applications

查看更多
人间绝色
5楼-- · 2018-12-31 05:30

The static key word in the main method is used because there isn't any instantiation that take place in the main method. But object is constructed rather than invocation as a result we use the static key word in the main method. In jvm context memory is created when class loads into it.And all static members are present in that memory. if we make the main static now it will be in memory and can be accessible to jvm (class.main(..)) so we can call the main method with out need of even need for heap been created.

查看更多
查无此人
6楼-- · 2018-12-31 05:31

If it wasn't, which constructor should be used if there are more than one?

There is more information on the initialization and execution of Java programs available in the Java Language Specification.

查看更多
看淡一切
7楼-- · 2018-12-31 05:31

there is the simple reason behind it that is because object is not required to call static method , if It were non-static method, java virtual machine creates object first then call main() method that will lead to the problem of extra memory allocation.

查看更多
登录 后发表回答