简体   繁体   中英

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

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 :

The method main must be declared public , static , and void . It must specify a formal parameter (§8.4.1) whose declared type is array of String .


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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM