Skip to content


Say “Hello World!” Without Main Method in Java

Can you print out “Hello World!” without writing a main method in Java? Think for a while. Yes, you’re right. It can be possible using “static initialization blocks”. Let’s see the source code first:

package net.cavdar.staticinitializer;

/**
* Says “Hello World!” without main method. A simple use of
* static initialisation.
*
* @author accavdar
*/
public class HelloWorldWithoutMain {
static {
System.out.println(“Hello World!”);
System.exit(0); // prevents “main method not found” error
}
}

So, what is “static initializer block”?

  • A static initializer block is defined using the keyword static.
  • The code in a static initializer block is executed once by the virtual machine when the class is loaded.
  • A static initializer block cannot contain a return statement. Therefore, no need to specify a return type.
  • A static initializer block doesn’t have an argument list.
  • It can initialize only static data members of the class.

Because the static initializer block is executed when the class is first loaded, we can print out “Hello World” without writing a main method. The execution is stopped using “System.exit()” command. So, we prevent “main method not found” error. It is tricky. Isn’t it?

Happy coding. :D

Note: Write fully qualified class name (for our example “net.cavdar.staticinitializer.HelloWorldWithoutMain”) for configuring run options for main class, if you need.

Related Posts

Posted in Java, Tips & Tricks.

Tagged with , , , .


22 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. jj says

    is this about learning java basics ? :)

  2. onotole says

    [:|||:]

  3. Nice trick says

    Trick is nice but what are you trying to preach; can it ever be used in real life scenario.

  4. Veera says

    Nice trick!

  5. Abdullah Cetin CAVDAR says

    Here, I use the traditional “Hello World” example to demonstrate the usage of “static initializer blocks”. More meaningful and detailed example can be found in the link below:

    The Essence of OOP using Java, Static Initializer Blocks

    Thank you all for your nice comments. :D

  6. abhi says

    It doesn’t work ! :)

  7. Abdullah Cetin CAVDAR says

    What is the problem Abhi?

  8. tugrul says

    just like old procedural languages
    good old days!!

  9. Kamal says

    Yes, you can print this. But static initializer is useful for initializing resources/variables shared by all instances of a class.

  10. kajastancos says

    your code is a bit buggy, please test on several different platforms, before posting, kthxbye

    package net.cavdar.staticinitializer;

    /**
    * Says “Hello World!” without main method. A simple use of
    * static initialisation.
    *
    * @author accavdar
    */
    public class HelloWorldWithoutMain {
    static {
    System.out.println(“Hello World!”);
    // you need the next line to avoid ClassNotFoundException
    new HelloWorldWithoutMain();
    System.exit(0); // prevents “main method not found” error
    }
    }

  11. Abdullah Cetin CAVDAR says

    Hi kajastancos,

    I run it for Windows, Fedora Linux and Sun Solaris operating systems without any modification and any problem. I checked it once more for you.

    Thanks for your comment and interest.

  12. Karahan ÖZTÜRK says

    It seems meaningless to say that static initialization is just a trick or it can’t be used in a real life scenario.
    It’s a common way to load libraries -though alternatives- when using jni.

  13. anshul mittal says

    thank you very much i desperately need it

  14. David Winthorpe says

    That’s neat but I’m not sure just exactly where we are supposed to go from here. Is this a basic step need to develop code further in java?

  15. Hayward Pool Heaters says

    Ok, so I just had to ask to see if anyone knows the answer.

    What was the first programming language to instigate the obligatory, “Hello World” as the first objective coding lesson?

    Would love to here if someone knows the answer to this, I have just been wondering for a long time now.

  16. lokanadham says

    very nice…

  17. Shabbir says

    It works…nice trick

  18. Boomiraj says

    Hi,
    This code is working correctly on the command promt compilation and execution . But its not working in eclipse… if u know please tell me ..

    Thanks and Regards,
    Boomiraj.P

    • tina says

      right click on the class name on the Package explorer (to left of screen) a menu list appears.. select Run as(towrds bottnm of list ) follwed by Run configurations..and change the text on Name field and that on Mainclass field by the name of the clasname tat u wanna run…thn clik Run on that window…:) happy coding

      • tina says

        this is how u run a pgm in eclipse without main();

  19. Product B Testimonials says

    I once published a blog on space travel and the first post was…wait for it….good bye world!

    Ha, had to share that one.

  20. Arindam says

    Will not work for java se 7!!!



Some HTML is OK

or, reply to this post via trackback.