sa.
sa.

Reputation: 511

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

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();
  }

}

Upvotes: 51

Views: 146003

Answers (7)

lamine ml
lamine ml

Reputation: 19

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();
    }
}

Upvotes: 0

Binary Nerd
Binary Nerd

Reputation: 13927

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);
    }
}

Upvotes: 45

foobar
foobar

Reputation: 619

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

Upvotes: 2

Kamran Siddiqui
Kamran Siddiqui

Reputation: 281

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);
    }
}

Upvotes: 28

mcobery
mcobery

Reputation: 19

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

Upvotes: 0

Dan Story
Dan Story

Reputation: 10155

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);
            }
        }

    }
}

Upvotes: -1

Gary
Gary

Reputation: 916

yes, but only if main is declared public

Upvotes: 2

Related Questions