Java Programming
C Programming
C++ Programming
How To

How to write java program without using main method?

151617

Answer

User Avatar
Wiki User
12/05/2009

Java program without main

We need a main method for executing a program.

But YES we can write a program without using main() method.

TRICK 1 of 2 :: while writing applets in java we don't use main... we use init() method instead.
TRICK 2 of 2 :: using 'static' we can write a program whic will execute successfully and output the desired message on screen. Here it is :: class Mohit{ static { System.out.println("This java program has run without the main method"); System.exit(0); } } -->save the program as Mohit.java to compile::javac Mohit.java (in command prompt) to run ::java Mohit(command prompt) output will be ::This java program has run without the main method

Whoa!!!!! we are done.


;)