8
class NewClass{
public static void main(String a){
    System.out.print("Hello");
}
}

When I'm trying to execute above code, then it shows an error, main method not found. But when I changed public static void main(String a) to public static void main(String... a) or public static void main(String a[]). Then, it works..!!

So, My question is how many different ways we can write legal main method signature and what this signature public static void main(String... a) means ?

Ravi
  • 28,657
  • 41
  • 110
  • 158
  • 1
    That is the expected behaviour. The signature of the real `main` mathod **must** be `public static void main(String... a)` or as it is equivalent, `public static void main(String[] a)`.You could have any amounts of functions called `main` - as long as their signature is different. **But** even then, **only** the one with the correct signature will be treated as an entry point to a Java application. – ppeterka Nov 28 '12 at 10:59
  • equivalent also `public static void main(String[] a)` – jlordo Nov 28 '12 at 11:00
  • Its basic of basic. Please, google java 'Hello world' example – Maxim Shoustin Nov 28 '12 at 11:01
  • because main method's perameter is string array not string variable. for two valid option you need to read following link. http://stackoverflow.com/questions/4089572/what-is-the-correct-java-main-method-parameters-syntax – KSHiTiJ Nov 28 '12 at 11:03
  • 1
    The main() method accepts a single parameter: an array of Strings. This parameter is the mechanism through which the runtime system passes command line arguments to your application. – KSHiTiJ Nov 28 '12 at 11:06
  • without reading and understanding the whole question, peoples are down-voting the question. – Ravi Nov 28 '12 at 11:07
  • why this question downvoted !! – anshulkatta May 23 '13 at 06:48

8 Answers8

13

Simply because that's the requirement of Java.

A main method/entry point to a program must be a method declared as public static void main(String[] args). Your method that was declared with a String parameter was similar but not compatible.

An array is not the same as a single String - if someone invoked Java with three command-line parameters, the JVM would create a three-element string array, and then how would it pass this into your method that only takes a single string?

So in that case you were trying to launch a Java program based on a class that did not have an main method to act as an entry point.

(The reason why String... works is because this is syntactic sugar for an array parameter, and compiles down to a method with the same signature.)

Andrzej Doyle
  • 97,637
  • 30
  • 185
  • 225
4

Finally, i found the answer of my question in Sun Certified Programmer for Java 6 book.

First question was, how many different legal ways of using main method?

Legal main method signatures are

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

what does (String... a) means ??

To declare a method using a var-args parameter, we need to follow with an ellipsis(...) then use space and then name of the array that will hold the parameter received. So, above term known as Variable Argument and which means 0 to many.

And, rules of using variable argument parameters is, must be the last parameter in the method signature and can have only one var-args in a method.

Eg:

void myfunc(String... a)              //legal
void myfunc(String a...)              //illegal
void myfunc(String a,int... b)         //legal
void myfunc(String... a,int b)        //illegal 
Ravi
  • 28,657
  • 41
  • 110
  • 158
3

Its default in java. java compiler expects an array of command line arguments. thats why you need to specify string args[] or String...

Debobroto Das
  • 796
  • 6
  • 16
  • so that means... we can declare array using **`...`** also. is it ?? – Ravi Nov 28 '12 at 11:09
  • its actually variable number of arguments. you can pass values to a function using this. but can't declare a normal array inside a function. – Debobroto Das Nov 28 '12 at 11:10
2

public static void main(String... a) --> Is a method with a variable argument list, that is internally(in the method) treated as an array.

Legal Main Method Signature:

public static void main(String a[])

static public void main(String a[])

public static void main(String[] a)

static public void main(String[] a)

public static void main(String... a)

static public void main(String... a)

2

All these are valid/legal declarations of the main function in Java.

public static void main(String[] args) {
    // code
}

static public void main(String[] args) {
    // code
}

static public void main(String args[]) {
    // code
}

public static void main(String[] MarkElliotZuckerberg) {
    // code
}

public static void main(String... NewYork) {
    // code
}

The points to remember is -

  • The keywords public and static are interchangeable but mandatory.
  • The parameter of the main method can take the var-args syntax.
  • The name can be anything..!

Just for practice.. :P ...These are examples of invalid main method declarations -

static void main(String[] args) {
    // public is missing
}

public void main(String args[]) {
    // static is missing
}

public static int main(String... Java) {
    // return type not void

    return 0;
}

public void Main(String args[]) {
    // "main" not "Main"
}

public void main(string args[]) {
    // "String" not "string"
}

public void main(String.. SayHi) {
    // Ellipses is 3 dots !
}

Some give errors, while others simply overload the main method... Hope this helps...! If it did, let me know by commenting..!

Source - Java Tutorials on Theory of Programming

Vamsi Sangam
  • 898
  • 11
  • 16
1

public static void main(String a[]) is the main entry point signature for a typical Java program. So you should get on with this method signature.

Juvanis
  • 25,000
  • 3
  • 61
  • 84
1

Java Runtime tries to find a method with name "main" with argument types "String[]". It is just like using reflection for finding a method with type arguments as String[].

Also String[] are used so that the runtime can pass the program arguments or Command Line arguments that are provided. It is like the runtime tokenizes the arguments with white space characters and then calls this method named "main".

The method main must be declared public, static, and void. It must accept a single argument that is an array of strings. This method can be declared as either

public static void main(String[] args)
or

public static void main(String... args)

You can also refer to Oracle Java specification documentation for more understanding

Chapter 12 Execution - Section 12.1.4 Invoke Test.main

Narendra Pathai
  • 38,384
  • 18
  • 73
  • 117
0

You need exactly the String[] args parameter (it's an array).

The reason is that you need to declare the main() method with a specified signature (and a method signature contains its name, number of its parameters and the types of the parameters).

So if you create a function which a different parameter type (String vs. String array), it is not recognized.

rlegendi
  • 9,744
  • 2
  • 35
  • 47