In Java, Can we call the main() method of a class from another class?
OR
How to call ‘public static void main(String[] args)’ method from our code?
These are some questions that usually puzzles a Java programmer. This article aims to provide an answer to these problems in a simple and efficient way.
As we know, the main() method for any Java application as the Java Run time environment calls the main() method first. So it is obvious that we don’t need to call the main() method by ourselves as it is already called when the program starts. But what if we want to call the main() method from somewhere in our program? That’s the problem.
Solution:
Though Java doesn’t prefer main() method called from somewhere else in the program, it does not prohibit one from doing it as well. So, in fact, we can call the main() method whenever and wherever we need to.
But calling the main() method from our code is tricky. It can lead to many errors and exceptions, such as:
- The main() method must be called from a static method only inside the same class.
Java
// Java method to show that the main() method // must be called from a static method only // inside the same class import java.io.*; class GFG { // The method that calls the main() method // Note that this method is not static void mainCaller() { System.out.println("mainCaller!"); // Calling the main() method main( null ); } // main() method public static void main(String[] args) { System.out.println("main"); // Calling the mainCaller() method // so that main() method is called externally mainCaller(); } } |
- Compilation Error in java code:
prog.java:27: error: non-static method mainCaller() cannot be referenced from a static context mainCaller(); ^ 1 error
- The main() method must be passed the String[] args while calling it from somewhere else.
Java
// Java method to show that the main() method // must be passed the String[] args // while calling it from somewhere else import java.io.*; class GFG { // The method that calls the main() method static void mainCaller() { System.out.println("mainCaller!"); // Calling the main() method // Note that no parameter is passed main(); } // main() method public static void main(String[] args) { System.out.println("main"); // Calling the mainCaller() method // so that main() method is called externally mainCaller(); } } |
- Compilation Error in java code:
prog.java:17: error: method main in class GFG cannot be applied to given types; main(); ^ required: String[] found: no arguments reason: actual and formal argument lists differ in length 1 error
- Calling the main() method will lead to an infinite loop as the memory stack knows to run only the main() method.
Java
// Java method to show that Calling the main() method // will lead to an infinite loop as the memory stack // knows to run only the main() method import java.io.*; class GFG { // The method that calls the main() method static void mainCaller() { System.out.println("mainCaller!"); // Calling the main() method main( null ); } // main() method public static void main(String[] args) { System.out.println("main"); // Calling the mainCaller() method // so that main() method is called externally mainCaller(); } } |
- RunTime Error in java code:
RunTime Error in java code :- Exception in thread "main" java.lang.StackOverflowError mainCaller! main mainCaller! main mainCaller! main . . .
The right way to this:
Example 1: Calling main() method externally from the same class
Java
// Java method to show Calling main() method // externally from the same class import java.io.*; class GFG { static int count = 0 ; // The method that calls the main() method static void mainCaller() { System.out.println("mainCaller!"); count++; // Calling the main() only 3 times if (count < 3 ) { // Calling the main() method main( null ); } } // main() method public static void main(String[] args) { System.out.println("main"); // Calling the mainCaller() method // so that main() method is called externally mainCaller(); } } |
Output:
main mainCaller! main mainCaller! main mainCaller!
Example 1: Calling main() method externally from another class
Java
// Java method to show Calling main() method // externally from another class import java.io.*; class GFG { static int count = 0 ; // The method that calls the main() method static void mainCaller() { System.out.println("mainCaller!"); count++; // Calling the main() only 3 times if (count < 3 ) { // Calling the main() method Test.main( null ); } } } class Test { // main() method public static void main(String[] args) { System.out.println("main"); // Calling the mainCaller() method // so that main() method is called externally GFG.mainCaller(); } } |
Output:
main mainCaller! main mainCaller! main mainCaller!