简体   繁体   English

公共void main(String [] args)是无效的Java main方法签名吗?

[英]Is public void main(String[] args) Invalid java main method signature ?

I compiled the following code through notepad 我通过记事本编译了以下代码

class MainClass{
  public void main(String args[]){
    System.out.println("Hello!");

  }
}

I didn't use static keyword and the above code executed, but when I did 我没有使用static关键字,而是执行了上面的代码,但是当我这样做时

javap MainClass

in the command prompt,I got the following output, 在命令提示符下,我得到以下输出,

class MainClass {
MainClass();
  public static void main(java.lang.String[]);
}

the keyword was added itself, so is 关键字本身已添加,因此也是如此

public void main(String args[])

also considered a valid signature? 还算有效签名?

As stated in JLS Sec 12.1.4 : JLS Sec 12.1.4中所述

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


I think you might have been looking at an out-of-date version of the class; 我认为您可能一直在查看该课程的过时版本; when I tried decompiling your code, static has not been added. 当我尝试反编译您的代码时,尚未添加static

Compiled from "MainClass.java"
class MainClass {
  MainClass();
  public void main(java.lang.String[]);
}

暂无
暂无

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

相关问题 代码签名和编写步骤 public static void main(String args[] ) - Code signature and steps to write public static void main(String args[] ) 对main方法的误解/“ public static void main(String [] args)”的观点 - Misunderstanding main method/the point of “public static void main(String[] args)” java Hello World 错误:在类名中找不到主方法,请将主方法定义为:public static void main(String[] args) - java Hello World Error: Main method not found in classname, please define the main method as: public static void main(String[] args) 在ActivityTime类中找不到主要方法,请将该主要方法定义为:public static void main(String [] args) - Main method not found in class ActivityTime, please define the main method as: public static void main(String[] args) 错误:在类mainGUI中找不到主要方法,请将该主要方法定义为:public static void main(String [] args) - Error: Main method not found in class mainGUI, please define the main method as: public static void main(String[] args) “错误:在Grad类中找不到主要方法,请将该主要方法定义为:public static void main(String [] args)” - “Error: Main method not found in class Grad, please define the main method as: public static void main(String[] args)” 错误:在 class 中找不到主要方法,请将主要方法定义为:public static void main(String[] args) - Error: Main method not found in class, please define the main method as: public static void main(String[] args) 错误:在类Text中找不到主要方法,请将该主要方法定义为:public static void main(String [] args) - Error: Main method not found in class Text, please define the main method as: public static void main(String[] args) 错误:在Binary类中找不到主要方法,请将该主要方法定义为:public static void main(String [] args) - Error: Main method not found in class Binary, please define the main method as: public static void main(String[] args) 错误:在TextBook类中找不到主要方法,请将该主要方法定义为:public static void main(String [] args) - Error: Main method not found in class TextBook, please define the main method as: public static void main(String[] args)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM