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
12
  • But it does not run "before anything else in the program". The whole argument is a fallacy, and what’s more, this isn’t the first answer mentioning it, nor even the second or third. Commented Jul 12, 2012 at 13:40
  • I'm sorry that my answer repeats what others have said; I only answered to the best of my understanding and from what I could find online. From the results that I've looked at there is no other reason as to why the main method is static; unless there's one deeply hidden somewhere perhaps that's the only answer there is. My understanding of Java is fairly basic, but I have heard the above reason (from professors, textbooks, etc) and never any other. Commented Jul 12, 2012 at 13:49
  • @Jesse M Your comment only makes sense if you didn't even consider reading the other answers first. Which by the way is not a far fetched thing to do. As you mentioned yourself, your understanding is fairly basic so it is very probable that somebody else already answered the question more competently. And your comment seems to be an rationalization in order to make your answer look better. It's an extraordinary claim that you have Java textbooks and professors that think what you claim and frankly I don't believe they do. (Any references?) Commented Jul 13, 2012 at 13:13
  • 1
    @KonradRudolph The top comments seem pretty reasonable. main() is used as an entry point to the program and there are several references on the Java website saying that it is supposed to be similar to how C/C++ have a main() function. Because Java is all Objects, it has to be static to avoid object instantiation. Having it static also lets it be loaded and executable into the JVM at runtime. I'm just regurgitating previous answers, but I'm wondering what you would consider a satisfying answer. I think the best you will get is "That's how they wanted it". Keep in mind the date Java was made. Commented Jul 13, 2012 at 21:39
  • 1
    @Jesse Spot-on. It’s entirely possible that it’s merely a matter of convention (although I hope that it’s not, that would be such a boring answer). My original interest in this question was because I thought that using a proper instance to represent the object "running application", and having the entry point be a method (or the constructor) of this class would be a much more obvious design, since Java was designed to be object oriented from the get-go, and since seemingly analogous objects (threads, via Runnable) in Java do use this design. Why the (apparent) exception here? Commented Jul 16, 2012 at 15:25

lang-java

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