next up previous
Next: 1.2.7.1 A Sample Java Up: 1.2 Java Mechanics Previous: 1.2.6 Static Members

1.2.7 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. During the program debugging and testing process, a programmer can test individual classes by including main methods containing test code and executing them as if they were root classes!

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. Classes are so flexible that they 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.

DrJava is more flexible than other Java environments because it allows the user to evaluate any program expression in the Interactions window. No main method is required as the entry point for execution.



 
next up previous
Next: 1.2.7.1 A Sample Java Up: 1.2 Java Mechanics Previous: 1.2.6 Static Members
Corky Cartwright
2000-01-07