Yes, we can run java class without main() method if we use static initalizer and having System.exit(0); statement at the end. Program will compile and execute.
public class Test{ static{ System.out.println("Hello Test"); System.exit(0); } |
But it can be done only upto JAVA 6 version. From Java-7 version the program will compile but not execute.
Java interview questions on main method
- Can we overload main() method in java?
- Can we declare main() method as private or protected or with no access modifier?
- Can we declare main method as non static in java?
- Why main() method must be static?
- Can we change return type of main() method?
- Can we run java class without main() method?
- Can we declare the main method as final?