Reputation: 2376
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
Upvotes: 2
Views: 460
Reputation: 12413
If you don't have any entry point, you cannot run anything. If you have many entry points (many main methods), user who starts your application controls which will be run by command line.
On the other hand static initialization blocks are executed when class is loaded. Controlling which class is loaded and the order of class loading (and of executing their static initialization blocks) is much harder to control. That's enough reason not to put to much logic to static initialization blocks. You make something harder to read and understand and in reward you receive something harder to use.
Upvotes: 0
Reputation: 88806
OK, I'll admit that I haven't tried it with a static initialized class, but wouldn't typing
java StaticInitializedClass
on the command line print out
Exception in thread "main" java.lang.NoSuchMethodError: main
since it doesn't have a main(String[])
method?
Upvotes: 2
Reputation: 2801
Let's not forget about maintainability. When someone else goes to change your code they are always going to look for a main() method to start the program. Unless there is some reason you can't get around (and I'm guessing there is not) then I would just use the main method.
Upvotes: 1
Reputation: 328744
The main reason why it's better to use main()
is that you can have several main methods in your application (in different classes). If you load a class, main()
isn't executed automatically while static
code blocks are always executed exactly once at the first time a class is loaded and there is no way to prevent that.
Upvotes: 2
Reputation: 1502226
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.
Upvotes: 6
Reputation: 4540
You can call main()
for other objects from anywhere else in your code as many times as you want. A static initializer is only going to ever run once. Also, you can pass parameters from the command line into main()
.
Two big plusses for main()
in my book.
Upvotes: 5