next up previous
Next: 1.2.7.2 Variations on the Up: 1.2.7 Java Programs Previous: 1.2.7 Java Programs

1.2.7.1 A Sample Java Program

The following simple program prints the String output

rules!
and stops:
class Motto {
  public static void main(String[] args) {
    System.out.println("Java rules!");  
  }
}
We will explain the various syntactic constructions used in this sample program later in the monograph.

Before this program can be executed, it must be compiled into a form suitable for machine execution called a class file Motto.class. The name of the class file generated for a class C is simply C.class.

In DrJava, Java class definitions are entered as program text in the Definitions window and compiled using the Compile button at the top of the window. This process produces a class file for each class following the usual naming convention, but class files are placed in a special directory that is not accessible to the programmer. In DrJava, a programmer is only concerned with program source code; the class files are invisible.

If a compiled Java class C contains a main method with the header

public static void main(String[] args) {
then it can be executed as a program in DrJava by entering the command
java C
in the Interactions window. Hence, after the Motto class has been entered in the Definitions window and successfully compiled using the Compile button, it can executed by entering the command
java Motto
in the Interactions window. Of course, evaluating the expression
Motto.main(null);
in the Interactions window will produce exactly the same results.


Finger Exercise: Using DrJava, define the Motto class given above and execute it as program.


next up previous
Next: 1.2.7.2 Variations on the Up: 1.2.7 Java Programs Previous: 1.2.7 Java Programs
Corky Cartwright
2000-01-07