Why is the Java main method static?

前端 未结 30 2179
离开以前
离开以前 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:07

    I think the keyword 'static' makes the main method a class method, and class methods have only one copy of it and can be shared by all, and also, it does not require an object for reference. So when the driver class is compiled the main method can be invoked. (I'm just in alphabet level of java, sorry if I'm wrong)

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

    The public keyword is an access modifier, which allows the programmer to control the visibility of class members. When a class member is preceded by public, then that member may be accessed by code outside the class in which it is declared.

    The opposite of public is private, which prevents a member from being used by code defined outside of its class.

    In this case, main() must be declared as public, since it must be called by code outside of its class when the program is started.

    The keyword static allows main() to be called without having to instantiate a particular instance of the class. This is necessary since main() is called by the Java interpreter before any objects are made.

    The keyword void simply tells the compiler that main() does not return a value.

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

    The main() method in C++, C# and Java are static
    Because they can then be invoked by the runtime engine without having to instantiate any objects then the code in the body of main() will do the rest.

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

    When you execute the Java Virtual Machine (JVM) with the java command,

    java ClassName argument1 argument2 ...
    

    When you execute your application, you specify its class name as an argument to the java command, as above

    the JVM attempts to invoke the main method of the class you specify

    —at this point, no objects of the class have been created.

    Declaring main as static allows the JVM to invoke main without creating an instance of the class.

    let's back to the command

    ClassName is a command-line argument to the JVM that tells it which class to execute. Following the ClassName, you can also specify a list of Strings (separated by spaces) as command-line arguments that the JVM will pass to your application. -Such arguments might be used to specify options (e.g., a filename) to run the application- this is why there is a parameter called String[] args in the main

    References:Java™ How To Program (Early Objects), Tenth Edition

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

    The protoype public static void main(String[]) is a convention defined in the JLS :

    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.

    In the JVM specification 5.2. Virtual Machine Start-up we can read:

    The Java virtual machine starts up by creating an initial class, which is specified in an implementation-dependent manner, using the bootstrap class loader (§5.3.1). The Java virtual machine then links the initial class, initializes it, and invokes the public class method void main(String[]). The invocation of this method drives all further execution. Execution of the Java virtual machine instructions constituting the main method may cause linking (and consequently creation) of additional classes and interfaces, as well as invocation of additional methods.

    Funny thing, in the JVM specification it's not mention that the main method has to be static. But the spec also says that the Java virtual machine perform 2 steps before :

    • links the initial class (5.4. Linking)
    • initializes it (5.5. Initialization)

    Initialization of a class or interface consists of executing its class or interface initialization method.

    In 2.9. Special Methods :

    A class or interface initialization method is defined :

    A class or interface has at most one class or interface initialization method and is initialized (§5.5) by invoking that method. The initialization method of a class or interface has the special name <clinit>, takes no arguments, and is void.

    And a class or interface initialization method is different from an instance initialization method defined as follow :

    At the level of the Java virtual machine, every constructor written in the Java programming language (JLS §8.8) appears as an instance initialization method that has the special name <init>.

    So the JVM initialize a class or interface initialization method and not an instance initialization method that is actually a constructor. So they don't need to mention that the main method has to be static in the JVM spec because it's implied by the fact that no instance are created before calling the main method.

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

    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.

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