Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Why is the Java main method static?

The method signature of a Java mainmethod is:

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

Is there a reason why this method must be static?

Answer*

Draft saved
Draft discarded
Cancel
17
  • 5
    Implementing an interface does not solve the instantiation problem. Commented Nov 15, 2011 at 18:59
  • 28
    I personally like that public static void main serves as a marker of an entry point – a public parameterless constructor doesn't scream out "This is probably an entry point!" in the same way. Commented May 12, 2012 at 16:58
  • 6
    @EdwinDalorzo - What would be gained by forcing the entry point class to be instantiated? Calling a static method places the least amount of burden on the class. It's free to instantiate itself if that makes more sense for your design. Commented Jul 14, 2012 at 19:48
  • 18
    "which constructor should be called?" How is that even conceivably a problem? The same "problem" exists for the decision which main to call. Weirdly enough (for you), the JVM manages this just fine. Commented Jul 17, 2012 at 19:50
  • 10
    The main method is always public because it has to be accessed by the runtime engine, the JVM. Commented Feb 28, 2013 at 18:48

lang-java

AltStyle によって変換されたページ (->オリジナル) /