Keywords: current

Next: 1.8.2.1 A Complete Java Up: 1.8 The Command Pattern Previous: 1.8.1 Static Members of

## 1.8.2 Complete Java Programs

Every complete Java program must contain a root class where execution can begin. A root class must contain a `main` method defined with the header

    public static void main(String[] args)

To execute a Java program, a user must identify the name of the root class for the program. In most Java computing environments, the command required to execute the Java program with root class C is simply

> `java C`

Note that a class may contain a `main` method even if it is not intended to be the root class of an actual program. Of course, evaluating the DrJava expression

> `Motto.main(null);`

is equivalent to executing this conssoled. For the sake of programming convenience, the DrJava Interactions pane accepts the command line syntax as well as the explicit invocation of the `main` method shown above.

In batch programming environments that lack an Interactions pane like DrJava, it is common practice to use `main` methods in non-root classes as test methods. Such test methods can easily be executed from the command line by typing

> `java C`

where `C` is the class being executed. We do not recommend this practice in DrJava because `DrJava` can evaluate any program expression in the Interactions pane, the testing process is even more convenient. For the moment, we recommend defining a static method `test()` in each class that tests all of the methods of the class. Later, we will revise this recommendation to describe a much more comprehensive testing strategy.

When execution begins in the `main` method of a root class, no instances of the root class exist. In fact, most Java programs never instantiate the root class because it serves as a framework for organizing program execution rather than a definition of a new form of data. Java classes really serve two distinct purposes: defining data objects and organizing static methods and variables.

The `args` parameter of the `main` method in the root class is used to pass command line options to the Java program. We will not use this feature in this monograph.

Subsections

- 1.8.2.1 A Complete Java Program

Next: 1.8.2.1 A Complete Java Up: 1.8 The Command Pattern Previous: 1.8.1 Static Members of