Why is the Java main method static?

前端 未结 30 2026
离开以前
离开以前 2020-11-22 01:41

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

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

Is the

相关标签:
30条回答
  • 2020-11-22 01:58

    Recently, similar question has been posted at Programmers.SE

    • Why a static main method in Java and C#, rather than a constructor?

      Looking for a definitive answer from a primary or secondary source for why did (notably) Java and C# decide to have a static method as their entry point – rather than representing an application instance by an instance of an Application class, with the entry point being an appropriate constructor?

    TL;DR part of the accepted answer is,

    In Java, the reason of public static void main(String[] args) is that

    1. Gosling wanted
    2. the code written by someone experienced in C (not in Java)
    3. to be executed by someone used to running PostScript on NeWS

     
    For C#, the reasoning is transitively similar so to speak. Language designers kept the program entry point syntax familiar for programmers coming from Java. As C# architect Anders Hejlsberg puts it,

    ...our approach with C# has simply been to offer an alternative... to Java programmers...

    ...

    0 讨论(0)
  • 2020-11-22 01:58

    The true entry point to any application is a static method. If the Java language supported an instance method as the "entry point", then the runtime would need implement it internally as a static method which constructed an instance of the object followed by calling the instance method.

    With that out of the way, I'll examine the rationale for choosing a specific one of the following three options:

    1. A static void main() as we see it today.
    2. An instance method void main() called on a freshly constructed object.
    3. Using the constructor of a type as the entry point (e.g., if the entry class was called Program, then the execution would effectively consist of new Program()).

    Breakdown:

    static void main()

    1. Calls the static constructor of the enclosing class.
    2. Calls the static method main().

    void main()

    1. Calls the static constructor of the enclosing class.
    2. Constructs an instance of the enclosing class by effectively calling new ClassName().
    3. Calls the instance method main().

    new ClassName()

    1. Calls the static constructor of the enclosing class.
    2. Constructs an instance of the class (then does nothing with it and simply returns).

    Rationale:

    I'll go in reverse order for this one.

    Keep in mind that one of the design goals of Java was to emphasize (require when possible) good object-oriented programming practices. In this context, the constructor of an object initializes the object, but should not be responsible for the object's behavior. Therefore, a specification that gave an entry point of new ClassName() would confuse the situation for new Java developers by forcing an exception to the design of an "ideal" constructor on every application.

    By making main() an instance method, the above problem is certainly solved. However, it creates complexity by requiring the specification to list the signature of the entry class's constructor as well as the signature of the main() method.

    In summary, specifying a static void main() creates a specification with the least complexity while adhering to the principle of placing behavior into methods. Considering how straightforward it is to implement a main() method which itself constructs an instance of a class and calls an instance method, there is no real advantage to specifying main() as an instance method.

    0 讨论(0)
  • 2020-11-22 01:58

    I don't know if the JVM calls the main method before the objects are instantiated... But there is a far more powerful reason why the main() method is static... When JVM calls the main method of the class (say, Person). it invokes it by "Person.main()". You see, the JVM invokes it by the class name. That is why the main() method is supposed to be static and public so that it can be accessed by the JVM.

    Hope it helped. If it did, let me know by commenting.

    0 讨论(0)
  • 2020-11-22 01:59

    From java.sun.com (there's more information on the site) :

    The main method is static to give the Java VM interpreter a way to start the class without creating an instance of the control class first. Instances of the control class are created in the main method after the program starts.

    My understanding has always been simply that the main method, like any static method, can be called without creating an instance of the associated class, allowing it to run before anything else in the program. If it weren't static, you would have to instantiate an object before calling it-- which creates a 'chicken and egg' problem, since the main method is generally what you use to instantiate objects at the beginning of the program.

    0 讨论(0)
  • 2020-11-22 02:00

    Because otherwise, it would need an instance of the object to be executed. But it must be called from scratch, without constructing the object first, since it is usually the task of the main() function (bootstrap), to parse the arguments and construct the object, usually by using these arguments/program parameters.

    0 讨论(0)
  • 2020-11-22 02:01

    This is just convention. In fact, even the name main(), and the arguments passed in are purely convention.

    When you run java.exe (or javaw.exe on Windows), what is really happening is a couple of Java Native Interface (JNI) calls. These calls load the DLL that is really the JVM (that's right - java.exe is NOT the JVM). JNI is the tool that we use when we have to bridge the virtual machine world, and the world of C, C++, etc... The reverse is also true - it is not possible (at least to my knowledge) to actually get a JVM running without using JNI.

    Basically, java.exe is a super simple C application that parses the command line, creates a new String array in the JVM to hold those arguments, parses out the class name that you specified as containing main(), uses JNI calls to find the main() method itself, then invokes the main() method, passing in the newly created string array as a parameter. This is very, very much like what you do when you use reflection from Java - it just uses confusingly named native function calls instead.

    It would be perfectly legal for you to write your own version of java.exe (the source is distributed with the JDK) and have it do something entirely different. In fact, that's exactly what we do with all of our Java-based apps.

    Each of our Java apps has its own launcher. We primarily do this so we get our own icon and process name, but it has come in handy in other situations where we want to do something besides the regular main() call to get things going (For example, in one case we are doing COM interoperability, and we actually pass a COM handle into main() instead of a string array).

    So, long and short: the reason it is static is b/c that's convenient. The reason it's called 'main' is that it had to be something, and main() is what they did in the old days of C (and in those days, the name of the function was important). I suppose that java.exe could have allowed you to just specify a fully qualified main method name, instead of just the class (java com.mycompany.Foo.someSpecialMain) - but that just makes it harder on IDEs to auto-detect the 'launchable' classes in a project.

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