在java中调用main内部的main方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21992659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:54:26  来源:igfitidea点击:

calling main method inside main in java

javamethodsmain

提问by abc

Can we call main method inside main?

我们可以在 main 中调用 main 方法吗?

public static void main(String[] args) {
    main({"a","b","c"});
}

Tried to google.Can't find the link. Sorry if the question is trivial

尝试谷歌。找不到链接。对不起,如果问题是微不足道的

采纳答案by Reimeus

You can but using the correct format

你可以但使用正确的格式

main(new String[] {"a","b","c"});

should give StackOverFlowError (not tested)

应该给 StackOverFlowError (未测试)

回答by Abimaran Kugathasan

You will get StackOverFlowError. If you call endless recursive calls/deep function recursions.

你会得到StackOverFlowError。如果您调用无休止的递归调用/深度函数递归。

Thrown when a stack overflow occurs because an application recurses too deeply.

由于应用程序递归太深而发生堆栈溢出时抛出。

You need to pass String array like

你需要像传递字符串数组

main(new String[] {"a","b","c"});

回答by Sarah

Kugathasan is right.

Kugathasan 是对的。

Yes it does give StackOverflow Exception. I tested it right now in Eclipse.

是的,它确实给了 StackOverflow 异常。我现在在 Eclipse 中对其进行了测试。

class CallingMain
{
    public static void main(String[] args)
    {
         main(new String[] {"a","b","c"});
    }
}

I have one suggestion, I think the best way to eliminate the confusion is to try coding and running it. Helps with lots of doubts.

我有一个建议,我认为消除混淆的最佳方法是尝试编码并运行它。帮助解决了很多疑问。

回答by John Red

You can. And it is very much legal.
Also, it doesn't necessarily give a StackOverFlowError:

你可以。这是非常合法的。
此外,它不一定会给出 StackOverFlowError:

public static void main(String args[]){

    int randomNum = (int)(Math.random()*10);

    if(randomNum >= 8)
    System.exit(0);

    System.out.print("Heyhey!");
    main(new String[]{"Anakin", "Ahsoka", "Obi-Wan"});

}

Just saying.

就是说。

回答by Anshuman Singh

You can simply pass the argument like main(new String[1])- this line will call main method of your program. But if you pass "new String[-1]" this will give NegativeArraySizeException. Here I'mm giving an example of Nested Inner Class where I am calling the Main method:

您可以简单地传递参数,例如main(new String[1])- 此行将调用程序的 main 方法。但是如果你传递“ new String[-1]”,这将给出 NegativeArraySizeException。这里我给出了一个嵌套内部类的例子,我在其中调用 Main 方法:

class Outer 
{
    static class NestedInner
    {

        public static void main(String[] args)
        {
            System.out.println("inside main method of Nested class");

        }//inner main
    }//NestedInner
    public static void main(String[] args) 
    {

        NestedInner.main(new String[1] );//**calling of Main method**
        System.out.println("Hello World!");
    }//outer main
}//Outer

Output:- inside main method of Nested class

输出:- 在嵌套类的 main 方法中

Hello World!

你好,世界!