Tuesday, March 23, 2010

public static void main(String... args)

Here you will see that you can even change the signature of main(). Although there is no reason for doing this, the canonical public static void main(String[] args) can be changed to public static void main(String ... args):

public class VarGreeter3 {

public static void printGreeting(String... names) {
for (String n : names) {
System.out.println("Hello " + n + ". ");
}
}

public static void main(String... args) {
printGreeting(args);
}
}
Run VarGreeter3 with the command:

java VarGreeter3 Paul Sue

and you will get the same output as before:

Hello Paul.
Hello Sue.

Using "main(String... args)" makes it easier to invoke main directly from Java. This can be useful for writing unit tests of your main() method.