📜  在Java中,我们可以从另一个类调用一个类的 main() 方法吗?

📅  最后修改于: 2022-05-13 01:54:25.731000             🧑  作者: Mango

在Java中,我们可以从另一个类调用一个类的 main() 方法吗?

在Java中,我们可以从另一个类调用一个类的 main() 方法吗?
或者
如何从我们的代码中调用 'public static void main(String[] args)' 方法?
这些是通常困扰Java程序员的一些问题。本文旨在以简单有效的方式为这些问题提供答案。
众所周知,作为Java运行时环境的任何Java应用程序的main()方法都会首先调用main()方法。所以很明显我们不需要自己调用 main() 方法,因为它在程序启动时已经被调用了。但是如果我们想从程序中的某个地方调用 main() 方法呢?那就是问题所在。
解决方案:
尽管Java不喜欢从程序的其他地方调用 main() 方法,但它也不禁止人们这样做。所以,事实上,我们可以随时随地调用 main() 方法。
但是从我们的代码中调用 main() 方法很棘手。它可能导致许多错误和异常,例如:

  • main() 方法只能在同一个类中从静态方法调用。
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();
    }
}


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


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


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


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


  • Java代码中的编译错误:
prog.java:27: error: non-static method mainCaller()
                     cannot be referenced
                     from a static context
        mainCaller();
        ^
1 error
  • main() 方法在从其他地方调用时必须传递 String[] 参数。

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();
    }
}
  • Java代码中的编译错误:
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
  • 调用 main() 方法将导致无限循环,因为内存堆栈知道只运行 main() 方法。

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();
    }
}
  • Java代码中的运行时错误:
RunTime Error in java code :-
 Exception in thread "main" java.lang.StackOverflowError

mainCaller!
main
mainCaller!
main
mainCaller!
main
.
.
.

正确的方法:
示例 1:从同一个类外部调用 main() 方法

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

输出:

main
mainCaller!
main
mainCaller!
main
mainCaller!

示例 1:从另一个类外部调用 main() 方法

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

输出:

main
mainCaller!
main
mainCaller!
main
mainCaller!