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:13

What is the meaning of public static void main(String args[])?

  1. public is an access specifier meaning anyone can access/invoke it such as JVM(Java Virtual Machine.
  2. static allows main() to be called before an object of the class has been created. This is neccesary because main() is called by the JVM before any objects are made. Since it is static it can be directly invoked via the class.

    class demo {    
        private int length;
        private static int breadth;
        void output(){
            length=5;
            System.out.println(length);
        }
    
        static void staticOutput(){
            breadth=10; 
            System.out.println(breadth);
        }
    
        public static  void main(String args[]){
            demo d1=new demo();
            d1.output(); // Note here output() function is not static so here
            // we need to create object
            staticOutput(); // Note here staticOutput() function is  static so here
            // we needn't to create object Similar is the case with main
            /* Although:
            demo.staticOutput();  Works fine
            d1.staticOutput();  Works fine */
        }
    }
    

    Similarly, we use static sometime for user defined methods so that we need not to make objects.

  3. void indicates that the main() method being declared does not return a value.

  4. String[] args specifies the only parameter in the main() method.

    args - a parameter which contains an array of objects of class type String.

查看更多
只若初见
3楼-- · 2018-12-31 05:13

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.

查看更多
公子世无双
4楼-- · 2018-12-31 05:14

It is just a convention. The JVM could certainly deal with non-static main methods if that would have been the convention. After all, you can define a static initializer on your class, and instantiate a zillion objects before ever getting to your main() method.

查看更多
春风洒进眼中
5楼-- · 2018-12-31 05:15

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.

查看更多
怪性笑人.
6楼-- · 2018-12-31 05:15

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

查看更多
牵手、夕阳
7楼-- · 2018-12-31 05:16

Let's simply pretend, that static would not be required as the application entry point.

An application class would then look like this:

class MyApplication {
    public MyApplication(){
        // Some init code here
    }
    public void main(String[] args){
        // real application code here
    }
}

The distinction between constructor code and main method is necessary because in OO speak a constructor shall only make sure, that an instance is initialized properly. After initialization, the instance can be used for the intended "service". Putting the complete application code into the constructor would spoil that.

So this approach would force three different contracts upon the application:

  • There must be a default constructor. Otherwise, the JVM would not know which constructor to call and what parameters should be provided.
  • There must be a main method1. Ok, this is not surprising.
  • The class must not be abstract. Otherwise, the JVM could not instantiate it.

The static approach on the other hand only requires one contract:

  • There must be a main method1.

Here neither abstract nor multiple constructors matters.

Since Java was designed to be a simple language for the user it is not surprising that also the application entry point has been designed in a simple way using one contract and not in a complex way using three independent and brittle contracts.

Please note: This argument is not about simplicity inside the JVM or inside the JRE. This argument is about simplicity for the user.


1Here the complete signature counts as only one contract.

查看更多
登录 后发表回答