Java Reference
In-Depth Information
public static void main(String args[])
From within main() , the various parameters are available as the elements
of the array of String s. The class in Example 4.1 will display those parameters
when the program is run.
Example 4.1 Java program to dump command-line arguments
/*
* simple command-line parameter displayer
*/
public class
CLine
{
public static void
main(String [] args)
{
for (int i = 0; i < args.length; i++)
{
System.out.println(args[i]);
}
} // main
} // class CLine
We compile and run the example, providing a few command-line
parameters:
$ javac CLine.java
$ java CLine hello world file.txt blue
hello
world
file.txt
blue
$
Not all classes will have main() methods, but any can. Even if several
classes in a package have main() methods, that is not a problem. Which one
will be the “main” main() ? It's the class we specified when we invoked our
program. In Example 4.1, the main() that is executed is the one in the CLine
Search WWH ::




Custom Search