简体   繁体   English

java 中的 public static void main(String arg[ ] ) 是否已修复?

[英]public static void main(String arg[ ] ) in java is it fixed?

I was recently asked in an exam if public static void main(String arg[]) format of main method was fixed?我最近在考试中被问到main方法的public static void main(String arg[])格式是否固定? Can we change it?我们可以改变它吗? Can we use main without any of public , static or void ?我们可以在没有publicstaticvoid的情况下使用main吗? If not, why is it not hard coded that main(String arg[]) would stand for public static void main(String arg[]) always?如果不是,为什么不硬编码main(String arg[]) String arg[]) 总是代表public static void main(String arg[])

The signature of the main method is specified in the Java Language Specifications section 12.1.4 and clearly states: main 方法的签名在Java Language Specifications section 12.1.4中指定并明确指出:

The method main must be declared public, static, and void.方法 main 必须声明为 public、static 和 void。 It must specify a formal parameter (§8.4.1) whose declared type is array of String.它必须指定一个形式参数(§8.4.1),其声明类型为字符串数组。

  • it must be public otherwise it would not be possible to call it它必须是public的,否则将无法调用它
  • it must be static since you have no way to instantiate an object before calling it它必须是static因为你无法在调用它之前实例化 object
  • the list of String arguments is there to allow to pass parameters when executing a Java program from the command line. String arguments 的列表允许在从命令行执行 Java 程序时传递参数。 It would have been possible to define it without arguments but is more practical like that (and similar to other languages)如果没有 arguments 就可以定义它,但这样更实用(并且类似于其他语言)
  • the return type is void since it does not make sense to have anything else: a Java program can terminate before reaching the end of the main method (eg, by calling System.exit() )返回类型为void因为它没有任何其他意义:Java 程序可以在到达 main 方法结束之前终止(例如,通过调用System.exit()

The method signature can therefore be:因此,方法签名可以是:

public static void main( String[] args )
public static void main( String... args )

note that the varargs version ( ... ) is only valid from Java 5请注意,可变参数版本 ( ... ) 仅从 Java 5 开始有效

As the Java language allows the brackets [] to be positioned after the type or the variable (the first is generally preferred),由于 Java 语言允许括号[]位于类型或变量之后(通常首选第一个),

public static void main( String args[] ) // valid but usually non recommended

is also valid也有效

If you look into JDK source code (jdk-src\j2se\src\share\bin\java.c):如果您查看 JDK 源代码 (jdk-src\j2se\src\share\bin\java.c):

/* Get the application's main method */
mainID = (*env)->GetStaticMethodID(env, mainClass, "main",
                   "([Ljava/lang/String;)V");
...
{    /* Make sure the main method is public */
...
mods = (*env)->CallIntMethod(env, obj, mid);
if ((mods & 1) == 0) { /* if (!Modifier.isPublic(mods)) ... */
    message = "Main method not public.";
    messageDest = JNI_TRUE;
    goto leave;
...

It becomes very clear that it must have only this signature.很明显,它必须只有这个签名。

Public is a kind of access specifier due to which we can access it from outside the class. Since main is the function that acts as an execution point. public 是一种访问说明符,因此我们可以从 class 外部访问它。因为 main 是充当执行点的 function。 Main function is called by the JVM which is outside the class so it must be declared as public. Main function 被 JVM 调用,它在 class 之外,因此必须声明为公共。

Static is a kind of keyword used in java to specify that there is no need to create any instance of it. Static 是 java 中使用的一种关键字,用于指定不需要创建它的任何实例。 As we know main method also reside inside a class and to access the particular function of a class from outside the class (In this case from JVM), an instance of that particular class is needed, so to avoid these we simply put static to make access main method outside of class.正如我们所知,main 方法也驻留在 class 中,并且要从 class 外部访问 class 的特定 function(在本例中来自 JVM),需要该特定 class 的实例,因此为了避免这些,我们只需将 make 882378076 放在访问 class 之外的主要方法。

Void is the return type since other return type in main method is meaningless. Void 是返回类型,因为 main 方法中的其他返回类型是没有意义的。

String is a pre-defined class name in java. And args [] is a variable of array types. String是java中预定义的class名称。args[]是数组类型的变量。 It's a variable name of String type object. So we can also change the name of args [].是一个String类型的变量名object。所以我们也可以改变args[]的名字。 String class and its object can be passed in a running program as an argument to pass information to the main method from command line.字符串 class 及其 object 可以作为参数传递到正在运行的程序中,以将信息从命令行传递给 main 方法。

The main method must be public so it can be found by the JVM when the class is loaded. main方法必须是public的,这样加载class的时候才能被JVM找到。 Similarly, it must be static so that it can be called after loading the class, without having to create an instance of it.同样,它必须是static ,以便在加载 class 后可以调用它,而不必创建它的实例。 All methods must have a return type, which in this case is void.所有方法都必须有一个返回类型,在本例中为void.

I cannot answer for the arguments of the method but it must be public because the jvm must be able to access the function and it must be static because the jvm does not know how to create an instance of your class.我无法回答该方法的 arguments 但它必须是公开的,因为 jvm 必须能够访问 function 并且它必须是 static 因为 jvm 不知道如何创建 88398112.195188 的实例

This post provides a good detailed answer about the reasoning for static: Why is the Java main method static?这篇文章提供了关于 static 推理的详细答案: Why is the Java main method static?

This post provides a good answer for why main is void: Why is main() in java void?这篇文章很好地回答了为什么 main 是无效的: Why is main() in java void?

If not, why is it not hard coded that main(String arg[]) would stand for public static void main(String arg[]) always?如果不是,为什么不硬编码 main(String arg[]) 总是代表 public static void main(String arg[]) ?

You can have methods called "main" with any signature and access you like.您可以拥有名为“main”的方法,带有您喜欢的任何签名和访问权限。 The special rules only apply to the method you want the JVM to call to start a program.特殊规则仅适用于您希望 JVM 调用以启动程序的方法。

public class Test {
  public static void main(String[] args) {
    StrangeMain m = new StrangeMain();
    System.out.println(m.main());
    System.out.println(m.main(new String[]{"aaa","bbb"}));
  }
}

class StrangeMain{
  protected String main(){
    return "xyzzy";
  }
  protected String main(String[] arg){
    return arg[0];
  }
}

compiles, runs, and outputs:编译、运行和输出:

xyzzy
aaa
             Public static void main(String [] ar)

To understand this we need to know the syntax of method and the array.要理解这一点,我们需要知道方法和数组的语法。

Syntax of method is:方法的语法是:

return type methodName返回类型方法名

so the main method is written along with void which is return type.所以 main 方法是和返回类型 void 一起写的。

Syntax of Array:数组语法:

datatype [] arrayName数据类型 [] 数组名

the square braces indicates whether it is the of dimension array.Since we have one pair of square braces it is one dimension array.方括号表示它是否是维数组。由于我们有一对方括号,它是一维数组。

The meaning of words in the main method: main方法中语句的含义:

Public: Public is the access specifier it is intended for the purpose of the JVM to execute the main method from any location. Public:Public 是访问说明符,目的是为了 JVM 从任何位置执行 main 方法。

Static: Static is a modifier.The main method must be declared as static so that the JVM can access the main method directly by using the class name. Static:Static是一个修饰符。main方法必须声明为static,这样JVM就可以通过class这个名字直接访问main方法。

When we execute a java program we use the class name so when we write static it will help the JVM to access the main method.当我们执行 java 程序时,我们使用 class 名称,因此当我们编写 static 时,它将帮助 JVM 访问 main 方法。

If we remove static then it becomes instance method,to access an instance method we should create and object and then invoke the method using the object reference.如果我们删除 static 然后它变成实例方法,访问实例方法我们应该创建和 object 然后使用 object 引用调用该方法。

void: Void is the return type of main method. void:void是main方法的返回类型。 The Caller of the main method is JVM and the JVM does not expect any value from the main method and there fore the main method should not return any value.This is the reason for specifying Void. main 方法的 Caller 是 JVM,而 JVM 不期望从 main 方法得到任何值,因此 main 方法不应该返回任何值。这就是指定 Void 的原因。

main: Main is the method name and it is fixed as per the Java coding conventions. main:Main 是方法名称,它是按照 Java 编码约定固定的。

String[]: It is used for storing the command line arguments.The name of the array can be any valid Java identifier. String[]:用于存放命令行arguments。数组名可以是任意有效的Java标识符。

So after String[] we can give name as any valid java identifier it can be 'ar' or it can be 'args'.因此,在 String[] 之后,我们可以将名称命名为任何有效的 java 标识符,它可以是“ar”或“args”。

You can change it if you create a new loader for your app.如果您为您的应用程序创建一个新的加载器,您可以更改它。 The public static void main( String args[] ) format is just the default solution people working on the JVM found to call your Java programs, so that there is a definite way to do it. public static void main( String args[] )格式只是默认解决方案,人们在 JVM 上工作时发现调用您的 Java 程序,因此有明确的方法可以做到这一点。

The real implementation we have today just uses the JNI interface to call the public static void main (String args[]) method by using this function , so you could easily write exactly the same code if you wanted using JNI and have a different method to load your app.我们今天的实际实现只是使用 JNI 接口调用public static void main (String args[])方法,通过使用这个 function ,所以如果你想使用 JNI 并且有不同的方法来实现,你可以轻松地编写完全相同的代码加载您的应用程序。

Here's an example in code that was taken from this page .这是取自此页面的代码示例

Here's the current linux launcher program, the method lookup starts here .这是当前的 linux 启动器程序,方法查找从这里开始

  • public-main() method must be used by any one of the outside the class as well as inside the class so its public public-main() 方法必须由 class 外部以及 class 内部的任何一个使用,因此它的公共

  • static-static is necessary bcoz in java if we define class than we define object for that class and than and only than we can use that class..but instead of this we directly use by write the word static如果我们定义 class 比我们为 class 定义 object 并且比我们可以使用 class 的 88213246945188 中的 static-static 是必需的..但是我们直接使用写字 882160788216078683

  • void-for main() cant return any value like int or char main()-main is the function or method which we can use for accessing the future of java String-in java all we write consider as a string args-arguments void-for main() 不能返回任何值,如 int 或 char main()-main 是 function 或我们可用于访问 java 的未来的方法 String-in java 我们编写的所有内容都视为字符串 args-arguments

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 public static void main(String arg[]) 中访问 main() - Accessing main() in public static void main(String arg[]) Java - public static void main() - Java - public static void main() 了解公共静态void main(String [] args) - Understanding public static void main(String[] args) Java拒绝public static void main方法,请求public static void main方法 - Java rejects public static void main method, requests public static void main method 对main方法的误解/“ public static void main(String [] args)”的观点 - Misunderstanding main method/the point of “public static void main(String[] args)” 如何在 Java 中调用公共 static void main(String[] args) class 中的方法? - How to call a method inside the public static void main(String[] args) class in Java? 我的带有简单println的java public static void main(String [] args)在android studio中不起作用 - my java public static void main(String []args) with a simple println does not work in android studio 是否可以在没有 public static void main(String[] args) 的情况下编写 Java 代码? - Is it possible to write Java code without public static void main(String[] args)? 使用 public static void main(String args[]) 从子 class JAVA 获取 output - Using public static void main(String args[]) to get output from child class JAVA 如何在不使用 public static void main (String[]args) 的情况下在 vs 代码中获取 Java 程序中的用户输入 - How to get user input in java programs in vs code without using public static void main (String[]args)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM