为什么 main() 在 java 中被声明为 public 和 static
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22328302/
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
Why main() is declared public and static in java
提问by AK4
Why is the main declared as public and static?
为什么 main 被声明为 public 和 static?
public static void main(String arg[])
{}
acording to ans in java
根据java中的ans
"The method is static because otherwise there would be ambiguity: which constructor should be called?"
采纳答案by Lucian Novac
public - The main method is called by the JVM to run the method which is outside the scope of the project therefore the access specifier has to be public to permit a call from anywhere outside the application.
public - JVM 调用 main 方法来运行项目范围之外的方法,因此访问说明符必须是 public 以允许从应用程序之外的任何地方调用。
static - When the JVM makes a call to the main method there is no object that exists for the class being called therefore it has to have static method to allow invocation from class.
static - 当 JVM 调用 main 方法时,被调用的类不存在对象,因此它必须具有静态方法以允许从类调用。
void - Java is a platform independent language, therefore if it returns some value, then the value may have a different meaning between different platforms so unlike C it can not assume a behavior of returning value to the operating system.
void - Java 是一种独立于平台的语言,因此如果它返回某个值,那么该值在不同平台之间可能具有不同的含义,因此与 C 不同,它不能假设向操作系统返回值的行为。