Hi friends,
How can we execute a program without main() in java? give me a example of it?
Thank you
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted May 24, 2012, 2:07 AM
You can write a runnable Java program which does not have main method at all. This can be done using the static block of the class.
The reason this works is that static initialization blocks get executed as soon as the class is loaded, even before the main method is called. During run time JVM will search for the main method after exiting from this block. If it does not find the main method, it throws an exception. To avoid the exception System.exit(0); statement is used which terminates the program at the end of the static block itself.
class MainMethodNot
{
static
{
System.out.println("This java program have run without the run method");
System.exit(0);
}
}
Please refer the below link
http://www.java-tips.org/java-se-tips/java.lang/how-to-write-a-java-application-without-a-main-m-2.html
Thanks