Home Segments Top Top Previous Next

549: Mainline

Now that you understand array parameters, you are, at last, ready to understand why the main method has a parameter declared by String arg[].

Evidently, the main method has just one parameter, arg, which is assigned, when the method is called, to an array of String instances. The length of the array is equal to the number of command-line arguments provided; each element corresponds to one command-line argument.

Thus, the following program displays all the command-line arguments provided when the Demonstrate program is called:

public class Demonstrate { 
 public static void main(String argv []) { 
  int max = argv.length; 
  for (int counter = 0; counter < max; ++counter) { 
   System.out.println(argv[counter]); 
  } 
 } 
}