Necessity for "main" (java)
I know its looks crazy. But, anyways. We all know the way main method should be :
public static void main(String args[])
. Reason is
public
: The method can be accessed from the code outside the class in which it is defined(invoked from JVM)static
: The method can be accessed without instantiating the class in which it is declared. Again, this keyword also allows the JVM to invoke this method withou开发者_高级运维t instantiating the class.void
: The method does not return any data.
So, whats the necessity for "main" here. Even if there is another method say public static void entry(String args[])
, why jvm will not invoke this method. It gives compiler issues saying it couldnt find the main method. Even this public static void entry(String args[])
compiles to the above mentioned 3 rules.
Go ahead. Implement your own command-line driver that uses the JNI invocation interface to launch a program at whatever entrypoint appeals to you.
The special character of 'main' is purely a question of the Java command-line program. The designers made a choice. Their reasons are not wildly interesting; it's not going to change. Well, to be precise, you could join the openjdk project and try to sell them a feature.
The method has to be called main
because there needs to be something to specify that the method is the entry point method. Not just any public static void
method with a single String[]
argument should be considered an entry point... you might want such a method that does something different. Also, by specifying a specific name the method must have, there is a guarantee that a single class can have at most a single entry point method. You could have:
class Something {
public static void main(String[] args) { ... }
public static void parseArgs(String[] args) { ... }
public static void displayInvalidArgsMessage(String[] args) { ... }
}
If you tried to run that class, how would it pick the method to use unless there were some rule? With annotations today, one option might be to require a public static void
method with a String[]
arg annotated with @EntryPoint
or some such... but the main
solution is still better than this, because the compiler can enforce the restriction of one main
method per class using its normal method signature rules, while multiple methods could be given the same annotation. Not to mention the fact that there needed to be a way to run programs long before annotations were added and the precedent from C and C++.
Apart from the reasons given by others, there was/is one more - nontechnical, but probably more important than the technical considerations -: making Java easy to learn (for C++ programmers, primarily). Back when Java started, their creators wanted to make it an easy language and help developers convert from existing languages. Since the primary existing OO language in use was C++ at that time, and there the entry point is called main
(which is indeed, as @Felix noted, inherited from C to make C++ backward compatible as much as possible), they used the same name in Java too. Note that since then, C# adopted the same convention as well (albeit in a somewhat more general form), for the same reason.
精彩评论