Why is the Java main method static?

前端 未结 30 2178
离开以前
离开以前 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 02:16

    Let me explain these things in a much simpler way:

    public static void main(String args[])
    

    All Java applications, except applets, start their execution from main().

    The keyword public is an access modifier which allows the member to be called from outside the class.

    static is used because it allows main() to be called without having to instantiate a particular instance of that class.

    void indicates that main() does not return any value.

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

    main() is static because; at that point in the application's lifecycle, the application stack is procedural in nature due to there being no objects yet instantiated.

    It's a clean slate. Your application is running at this point, even without any objects being declared (remember, there's procedural AND OO coding patterns). You, as the developer, turn the application into an object-oriented solution by creating instances of your objects and depending upon the code compiled within.

    Object-oriented is great for millions of obvious reasons. However, gone are the days when most VB developers regularly used keywords like "goto" in their code. "goto" is a procedural command in VB that is replaced by its OO counterpart: method invocation.

    You could also look at the static entry point (main) as pure liberty. Had Java been different enough to instantiate an object and present only that instance to you on run, you would have no choice BUT to write a procedural app. As unimaginable as it might sound for Java, it's possible there are many scenarios which call for procedural approaches.

    This is probably a very obscure reply. Remember, "class" is only a collection of inter-related code. "Instance" is an isolated, living and breathing autonomous generation of that class.

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

    Basically we make those DATA MEMBERS and MEMBER FUNCTIONS as STATIC which are not performing any task related to an object. And in case of main method, we are making it as an STATIC because it is nothing to do with object, as the main method always run whether we are creating an object or not.

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

    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?

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

    static - When the JVM makes a call to the main method there is no object that exists for the class being called therefore it has to have static method to allow invocation from class.

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

    It is just a convention as we can see here:

    The method must be declared public and static, it must not return any value, and it must accept a String array as a parameter. By default, the first non-option argument is the name of the class to be invoked. A fully-qualified class name should be used. If the -jar option is specified, the first non-option argument is the name of a JAR archive containing class and resource files for the application, with the startup class indicated by the Main-Class manifest header.

    http://docs.oracle.com/javase/1.4.2/docs/tooldocs/windows/java.html#description

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