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

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)

查看更多
何处买醉
3楼-- · 2018-12-31 05:16

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.

查看更多
ら面具成の殇う
4楼-- · 2018-12-31 05:16

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.

查看更多
步步皆殇っ
5楼-- · 2018-12-31 05:17

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

查看更多
何处买醉
6楼-- · 2018-12-31 05:18

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

http://i.stack.imgur.com/qcmzP.png

 
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...

...

查看更多
回忆,回不去的记忆
7楼-- · 2018-12-31 05:19

The method is static because otherwise there would be ambiguity: which constructor should be called? Especially if your class looks like this:

public class JavaClass{
  protected JavaClass(int x){}
  public void main(String[] args){
  }
}

Should the JVM call new JavaClass(int)? What should it pass for x?

If not, should the JVM instantiate JavaClass without running any constructor method? I think it shouldn't, because that will special-case your entire class - sometimes you have an instance that hasn't been initialized, and you have to check for it in every method that could be called.

There are just too many edge cases and ambiguities for it to make sense for the JVM to have to instantiate a class before the entry point is called. That's why main is static.

I have no idea why main is always marked public though.

查看更多
登录 后发表回答