This question was asked to someone I know. What I could think of is that main method will be required to accept command line arguments as the method parameters.
Are there any other arguments in defense of public static void main(String args[])
method?
The thought of type initialization for the "main" class blocking until the application has finished is abhorrent.
Could we cope with it? I dare say. But I suspect I would always end up writing:
public class EntryPoint
{
static
{
// Workaround for entry points being static initializers
String[] arguments = getArgumentsHoweverThatHappens();
RealEntryPoint.execute(arguments);
}
}
... and nothing else would ever touch EntryPoint
.
Static initializers and the main method have different intents. The main method's purpose is to be invoked if, and only if, the JVM is called with the containing class as the main class (or if it's called directly by code). The purpose of static initialiers is to do class initialization. Initializers are always run, but it's possible to have main methods that are not.
In addition to above stated, the need for main
(not the characteristics of static blocks) is that your application needs a starting point, thats is, when you execute your application, you pass the JVM dozens of classes and the JVM needs to know which method to invoke first in order to start and execute your application. You need to declare which point is the beginning of your application because the JVM can't guess it. (Sorry for my english)
Static blocks are meant to be run once the corresponding class is loaded. The main()
, however, is the entry point to your program and as Jon said, it can be invoked multiple times.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With