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

或者在代码中调用 ‘public static void main(String[] args)‘ 方法?

这些问题通常会让我们感到困惑。这篇文章旨在以简单高效的方式解答这些问题。

正如我们所知,对于任何 Java 应用程序,Java 运行时环境首先会调用 main() 方法。因此,显而易见的是,我们不需要自己手动调用 main() 方法,因为程序启动时它就已经被调用了。但是,如果我们想从程序的某个地方调用 main() 方法呢?这就是问题所在。

解决方案:

尽管 Java 并不倾向于从程序的其他位置调用 main() 方法,但它也并没有禁止我们这样做。所以,实际上,我们可以在任何需要的时候和地点调用 main() 方法。

不过,从我们的代码中调用 main() 方法是很棘手的。它可能会导致许多错误和异常,例如:

  • main() 方法只能从同一个类中的静态方法调用。
// 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 代码中的编译错误:
prog.java:27: error: non-static method mainCaller()
                     cannot be referenced
                     from a static context
        mainCaller();
        ^
1 error
  • 从其他地方调用 main() 方法时,必须传递 String[] args 参数。
// 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 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 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:

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。如需转载,请注明文章出处豆丁博客和来源网址。https://shluqu.cn/34629.html
点赞
0.00 平均评分 (0% 分数) - 0