Previous | Next | Trail Map | Writing Java Programs | Table of Contents


The Anatomy of a Java Application

Java programs that run within a Java-compatible browser are known as applets. This lesson discusses the components of a stand-alone Java program, that is, a program written in the Java language that runs independently of any browser. Stand-alone Java programs are also known as Java applications. For a quick, no-nonsense introduction to writing applets, see The "Hello World" Applet .

With all the fuss being made about object-oriented programming and design, you might think that you need a Ph.D. to sort through it. Fear not. Along with descriptions of the components of a Java program, this lesson provides a simple introduction to the fundamental concepts of object-oriented programming--classes and objects-- through a simple Java application.

Print the Current Date and Time

The following application, called DateApp, displays the current date and time.
import java.util.Date;
class DateApp {
    public static void main (String args[]) {
        Date today = new Date(); 
        System.out.println(today);
    }
}
The program is a modified version of the canonical "Hello World" application found in The "Hello World" Application . The next page of this lesson provides a Comparison of DateApp and "Hello World".

A Note About Import

The first line in the program imports the Date class from the java.util package. Importing a class from a package makes that class available to the file into which it was imported. The java.util package is a collection of classes that provide miscellaneous functionality. The Date class from the java.util package lets you manage and manipulate calendar dates in a system independent way.

Package Tour provides an overview of all of the packages shipped as part of the Java development environment and the classes contained within each. Also, Creating and Using Packages discusses how to use packages and their objects and how to create your own.

Defining a Class

In the Java language, all functions and variables exist within a class or an object-- the Java language does not support global functions or stray variables. So, the skeleton of any Java application is a class definition.

The main() Method

The brain of any Java application is its main() method. When you run a Java application with the Java interpreter, you specify the name of the class that you want to run. The interpreter invokes the main() method defined within that class. The main() method controls the flow of the program, allocates whatever resources are needed, and runs any other methods that provide the functionality for the application.

An Introduction to Java Objects

The other corporeal components of a Java application are the supporting objects, classes, methods and Java language statements that you write to implement the application.

Saving, Compiling and Running an Application

Type in the program exactly as it appears above and save it to a file. Then use the Java compiler to compile the program, and the Java interpreter to run it.


Previous | Next | Trail Map | Writing Java Programs | Table of Contents