Executing a java program without main()

This question is asked by many interviewers during the technical interview. So, the question is self-explanatory. We have to execute a java program without using

We do this by using a static block or static initializer block. A static block is always executed before main(). Also a point to note that a static block does not have a return type and it does not accept any arguments.
Another important point to be noted is that one should never forget to use

in the static block to prevent no method found exception.

Output:
java program without main

The following two tabs change content below.

4 thoughts on “Executing a java program without main()

  1. >java StaticDemo
    Error: Main method not found in class StaticDemo, please define the main method as:
    public static void main(String[] args)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">