Procedure to run Java program without main method
Hi All,
I have read in lots of JAVA programming book that each JAVA program should have "main method", As execution of the program starts from the "main method" of the program. And we can't ignore main method
I want to know the is it possible to create & execute JAVA program code without "main method" ? Please suggest me....
Re: Procedure to run Java program without main method
Yes, It is possible to create Java program code without "main method".
See below example, Try to run this program it's running successfully without "main method":
Quote:
public class NoMain_Demo
{
static
{
System.out.println("Program without main method");
System.exit(0);
}
}
public class NoMain_demoTest
{
public static void main(String[] args)
{
new NoMain_Demo();
}
}
Re: Procedure to run Java program without main method
Hi friends,
By using the method known as "System.exit(0)" within the "Static Block" we can execute Java code without "main method". See something below can help you:
Program:
Quote:
class ABC
{
static
{
System.out.println("Java without main() method");
System.exit(0);
}
}
Re: Procedure to run Java program without main method
Hi,
According to the my Java knowledge we are able to run the java program without the "main() method". It's done using "Static block", As it's execution is done before "main() method". So when you run the java program it will first execute the content from the static block and the execution will stop after the "System.exit() method". Please refer the below example of the static block:
Code:
Quote:
static
{
System.out.println("Java program");
System.out.print("program execute successfully");
}
System.exit();
}