50

Can a main() method of class be invoked in another class in java?

e.g.

class class1{

  public static void main(String []args){

  }

}

class class2{

  public static void main(String []args){
      class1.main();
  }

}
Satish_Mhetre
  • 192
  • 1
  • 2
  • 17
sa.
  • 501
  • 1
  • 4
  • 3

7 Answers7

45

If you want to call the main method of another class you can do it this way assuming I understand the question.

public class MyClass {

    public static void main(String[] args) {

        System.out.println("main() method of MyClass");
        OtherClass obj = new OtherClass();
    }
}

class OtherClass {

    public OtherClass() {

        // Call the main() method of MyClass
        String[] arguments = new String[] {"123"};
        MyClass.main(arguments);
    }
}

kohane15
  • 809
  • 12
  • 16
Binary Nerd
  • 13,872
  • 4
  • 42
  • 44
  • 24
    for those who may or may not notice... this will create an infinite loop – Don Cheadle Nov 20 '14 at 16:28
  • 4
    @mmcrae This is recursion, not loop. Recursion is never infinite. – Saraph Dec 31 '14 at 02:04
  • 20
    wait what...why would recursion not be infinite? You could blow your stack, but that is just a practical limitation, mathematical recursion can of course be infinite if you never make progress towards a base case (or no base case exists). – JPC Feb 08 '16 at 18:55
  • 1
    @JPC This is not mathematical recursion. It is computer programming. All resources are finite. – user207421 Nov 26 '19 at 21:07
25

if I got your question correct...

main() method is defined in the class below...

public class ToBeCalledClass{

   public static void main (String args[ ]) {
      System.out.println("I am being called");
   }
}

you want to call this main method in another class.

public class CallClass{

    public void call(){
       ToBeCalledClass.main(null);
    }
}
zohar
  • 2,298
  • 13
  • 45
  • 75
Kamran Siddiqui
  • 251
  • 3
  • 2
2

yes, but only if main is declared public

Gary
  • 926
  • 3
  • 12
  • 24
  • 7
    ... or protected, or package private, or you do tricky things with reflection to allow it to be invoked despite being private. – Stephen C Mar 31 '10 at 04:13
1

As far as I understand, the question is NOT about recursion. We can easily call main method of another class in your class. Following example illustrates static and calling by object. Note omission of word static in Class2

class Class1{
    public static void main(String[] args) {
        System.out.println("this is class 1");
    }    
}

class Class2{
    public void main(String[] args) {
        System.out.println("this is class 2");
    }    
}

class MyInvokerClass{
    public static void main(String[] args) {

        System.out.println("this is MyInvokerClass");
        Class2 myClass2 = new Class2();
        Class1.main(args);
        myClass2.main(args);
    }    
}

Output Should be:

this is wrapper class

this is class 1

this is class 2

foobar
  • 571
  • 1
  • 5
  • 20
0

Yes as long as it is public and you pass the correct args. See this link for more information. http://www.codestyle.org/java/faq-CommandLine.shtml#mainhost

mcobery
  • 19
  • 4
0

try this 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();
    }
}
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
lamine ml
  • 19
  • 1
  • 3
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 03 '21 at 14:43
-1

Sure. Here's a completely silly program that demonstrates calling main recursively.

public class main
{
    public static void main(String[] args)
    {
        for (int i = 0; i < args.length; ++i)
        {
            if (args[i] != "")
            {
                args[i] = "";
                System.out.println((args.length - i) + " left");
                main(args);
            }
        }

    }
}
Dan Story
  • 9,985
  • 1
  • 23
  • 27