Tanvi
Tanvi

Reputation: 95

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?

Upvotes: 2

Views: 1230

Answers (1)

Andy Turner
Andy Turner

Reputation: 140318

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[]);
}

Upvotes: 5

Related Questions