java java中的main方法为final

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26710891/
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-11-02 10:27:24  来源:igfitidea点击:

main method as final in java

javamain

提问by Gpar

I saw this code in one of the certification exams:

我在其中一项认证考试中看到了此代码:

public class SimpleClass 
{
    int num;
    final static void main(final String args[])
    {
        String s1="new";
        String s2="String";
        String s3="Creation";
        System.out.println(s1+s2+s3);
    }
}

I know that final methods are ones which are not possible to override. I also know that if the usual signature of the mainmethod is altered, it will be treated as any other ordinary method by the JVM, and not as main().

我知道最终方法是无法覆盖的方法。我也知道,如果方法的通常签名 main被更改,JVM 会将其视为任何其他普通方法,而不是main().

However, the options given to me were:

然而,给我的选择是:

1>  Code  won't  compile
2>  Code  will  throw  an  exception
3>  will  print  newStringCreation.

It's not possible to run this program on eclipse IDE. Can anyone explain what should be the answer and why?

无法在 Eclipse IDE 上运行此程序。任何人都可以解释应该是什么答案以及为什么?

Ok let me put my question like this - When I execute my program, what will happen? Which of the 3 options above should I choose?

好的,让我这样提出我的问题 - 当我执行我的程序时,会发生什么?我应该选择以上 3 个选项中的哪一个?

回答by Eran

final static void mainwon't run, since main is not public.

final static void main不会运行,因为 main 不是公开的。

public final static void mainwill work.

public final static void main将工作。

At least that's the behavior on my Eclipse IDE.

至少这是我的 Eclipse IDE 上的行为。

回答by hchawla

The Code will compile without any problems but it will throw a run-time exception saying "main method not public". The main method has to be public because it has to be called by JVM which is outside the scope of the package and hence would need the access specifier-public. If you are unable to run it in eclipse, try the archaic method of saving the file in a notepad with filename.java. Go to cmd and reach the file location..If on desktop, use cd desktop! Use the following commands to run the file-

代码将编译没有任何问题,但它会抛出一个运行时异常说“主要方法不公开”。main 方法必须是公共的,因为它必须由包范围之外的 JVM 调用,因此需要访问说明符公共。如果您无法在 Eclipse 中运行它,请尝试使用 filename.java 将文件保存在记事本中的古老方法。转到cmd并到达文件位置..如果在桌面上,请使用cd桌面!使用以下命令运行文件-

javac filename.java

java filename

You will see the required run-time exception that I mentioned above.

您将看到我上面提到的所需的运行时异常。

回答by Stefan Freitag

The mainmethod has to be accessible from the outside. Hence, in your case the application will compile but throw an execution at runtime.

main方法必须可以从外部访问。因此,在您的情况下,应用程序将编译但在运行时抛出执行。

回答by Amz

You have the main method but since the modifier is final JVM wont be able to run the main method of the program.You wont see any compilation error.

您有 main 方法,但由于修饰符是 final,JVM 将无法运行程序的 main 方法。您不会看到任何编译错误。

You can run the program in eclipse when you make the modifier change of finalto public

当您将final修饰符更改为public时,您可以在 eclipse 中运行该程序