The entry point into a program in java is typically something like this
// MyMain.java
public class MyMain{
//whatever
public static void main(String args[]){
System.out.println("balderdash");
}
}
However, since there is only the one SOP in main, the above class may be like this instead
// MyMain.java
public class MyMain{
//whatever
static {
System.out.println("balderdash");
}
}
One obvious advantage to using main(...) is that arguments may be passed in to the program. Another ( I'm guessing here ) may have to do with the garbage collecter treating objects created within a static block differently.
What other benefits come from using the language-defined entry-point - public static void main( String args[] ) instead of using a static initializer.
p.s. The above snippets are for illustration only, and may not be compilable
EDIT: Here's an example of that problem:
class SampleTask implements Runnable
{
public void run()
{
System.out.println("Calling someStaticMethod");
Test.someStaticMethod();
System.out.println("someStaticMethod returned");
}
}
public class Test
{
static
{
System.out.println("Starting new thread...");
new Thread(new SampleTask()).start();
System.out.println("Thread started. Sleeping");
try
{
Thread.sleep(5000);
}
catch (InterruptedException e)
{
System.out.println("Interrupted!");
}
System.out.println("Finished sleeping");
}
public static void someStaticMethod()
{
System.out.println("In someStaticMethod");
}
public static void main(String[] args)
{
System.out.println("In main. Sleeping again.");
try
{
Thread.sleep(5000);
}
catch (InterruptedException e)
{
System.out.println("Interrupted!");
}
}
}
Output:
Starting new thread...
Thread started. Sleeping
Calling someStaticMethod
Finished sleeping
In someStaticMethod
someStaticMethod returned
In main. Sleeping again.
Note how the call to someStaticMethod
from the new thread has to wait until the static initializer has finished before it gets to run.