In Java, Can we call the main() method of a class from another class?

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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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 mainCalller() method
            // so that main() methiod is called externally
            mainCaller();
        }
    }

    chevron_right

    
    

    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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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 mainCalller() method
            // so that main() methiod is called externally
            mainCaller();
        }
    }

    chevron_right

    
    

    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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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 mainCalller() method
            // so that main() methiod is called externally
            mainCaller();
        }
    }

    chevron_right

    
    

    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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 mainCalller() method
        // so that main() methiod is called externally
        mainCaller();
    }
}

chevron_right


Output:

main
mainCaller!
main
mainCaller!
main
mainCaller!

Example 1: Calling main() method externally from another class

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 mainCalller() method
        // so that main() methiod is called externally
        GFG.mainCaller();
    }
}

chevron_right


Output:

main
mainCaller!
main
mainCaller!
main
mainCaller!



My Personal Notes arrow_drop_up

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.