Search code examples
javastatic-initializerentry-point

What benefits does main(...) provide over using a static-initializer as a pseudo entry-point?


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


Solution

    • You can test it or call it from other applications.
    • It's what other people will expect.
    • If you execute your whole application in the context of a static initializer, I suspect you'll effectively be holding a lock on that type for the whole time. If any other thread tries to call a static method in the same class, it will block. That's the sort of problem you get when you try to use a concept in an unexpected way - it's just conceptually "wrong". Initializing the class simply isn't the same as running an application.

    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.