3.7 The main() Method
The mechanics of compiling and running Java applications using the JDK are outlined in §1.10, p. 16. The java command executes a method called main in the class specified on the command line. Any class can have a main() method, but only the main() method of the class specified in the java command starts the execution of a Java application.
The main() method must have public accessibility so that the JVM can call this method (§4.7, p. 123). It is a static method belonging to the class, so that no object of the class is required to start the execution (§4.8, p. 132). It does not return a value; that is, it is declared as void (§6.4, p. 224). It always has an array of String objects as its only formal parameter. This array contains any arguments passed to the program on the command line (see the next subsection). The following method header declarations fit the bill, and any one of them can be used for the main() method:
public static void main(String[] args) // Method header public static void main(String... args) // Method header
The three modifiers can occur in any order in the method header. The requirements given in these examples do not exclude specification of additional modifiers (§4.8, p. 131) or any throws clause (§6.9, p. 251). The main() method can also be overloaded like any other method (§3.2, p. 52). The JVM ensures that the main() method having the previously mentioned method header is the starting point of program execution.
Program Arguments
Any arguments passed to the program on the command line can be accessed in the main() method of the class specified on the command line:
>java Colors red green blue
These arguments are called program arguments. Note that the command name, java, and the class name Colors are not passed to the main() method of the class Colors, nor are any other options that are specified on the command line passed to this method.
Since the formal parameter of the main() method is an array of String objects, individual String elements in the array can be accessed by using the [] operator.
In Example 3.12, the three arguments red, green, and blue can be accessed in the main() method of the Colors class as args[0], args[1], and args[2], respectively. The total number of arguments is given by the field length of the String array args. Note that program arguments can be passed only as strings, and must be explicitly converted to other values by the program, if necessary.
When no arguments are specified on the command line, an array of zero String elements is created and passed to the main() method. Thus the reference value of the formal parameter in the main() method is never null.
Program arguments supply information to the application, which can be used to tailor the runtime behavior of the application according to user requirements.
Example 3.12 Passing Program Arguments
public class Colors { public static void main(String[] args) { System.out.println("No. of program arguments: " + args.length); for (int i = 0; i < args.length; i++) System.out.println("Argument no. " + i + " (" + args[i] + ") has " + args[i].length() + " characters."); } }
Running the program:
>java Colors red green blue No. of program arguments: 3 Argument no. 0 (red) has 3 characters. Argument no. 1 (green) has 5 characters. Argument no. 2 (blue) has 4 characters.