【问题标题】:Can a main() method of class be invoked from another class in java可以从java中的另一个类调用类的main()方法吗
【发布时间】:2011-02-02 18:23:09
【问题描述】:

类的main()方法可以在java中的另一个类中调用吗?

例如

class class1{

  public static void main(String []args){

  }

}

class class2{

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

}

【问题讨论】:

    标签: java


    【解决方案1】:

    是的,但前提是 main 被声明为 public

    【讨论】:

    • ... 或受保护,或包私有,或者你用反射做一些棘手的事情,以允许它被调用,尽管它是私有的。
    【解决方案2】:

    当然。这是一个完全愚蠢的程序,它演示了递归调用main

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

    【讨论】:

    • 至少不要将Strings 与==(或!=)进行比较。
    【解决方案3】:

    是的,只要它是公开的并且您传递了正确的参数。 有关更多信息,请参阅此链接。 http://www.codestyle.org/java/faq-CommandLine.shtml#mainhost

    【讨论】:

      【解决方案4】:

      如果你想调用另一个类的 main 方法,假设我理解这个问题,你可以这样做。

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

      【讨论】:

      • 对于那些可能会或可能不会注意到的人......这将创建一个无限循环
      • @mmcrae 这是递归,不是循环。递归永远不会是无限的。
      • 等等……为什么递归不是无限的?您可能会破坏您的堆栈,但这只是一个实际限制,如果您从未在基本情况(或不存在基本情况)方面取得进展,数学递归当然可以是无限的。
      • @JPC 这不是数学递归。它是计算机编程。所有资源都是有限的。
      【解决方案5】:

      如果我的问题是正确的......

      main() 方法在下面的类中定义...

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

      你想在另一个类中调用这个主方法。

      public class CallClass{
      
          public void call(){
             ToBeCalledClass.main(null);
          }
      }
      

      【讨论】:

      • great.i 想知道将什么作为main 参数!简单来说就是null
      【解决方案6】:

      据我了解,问题不在于递归。我们可以轻松地调用您班级中另一个班级的main 方法。以下示例说明了静态和按对象调用。注意Class2中字static的省略

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

      输出应该是:

      这是包装类

      这是1级

      这是2级

      【讨论】:

        【解决方案7】:

        试试这个代码

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

        【讨论】:

        • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
        猜你喜欢
        • 2023-02-20
        • 1970-01-01
        • 2019-06-13
        • 1970-01-01
        • 1970-01-01
        • 2013-10-08
        • 1970-01-01
        • 1970-01-01
        • 2015-08-08
        相关资源
        最近更新 更多