Previous | Next | Trail Map | Writing Java Programs | The Anatomy of a Java Application


An Introduction to Java Objects

import java.util.Date;
class DateApp {
    public static void main(String args[]) {
        Date today = new Date();
        System.out.println(today);
    }
}
The DateApp application is about the simplest Java program that you can write that actually does something, and because it is such a simple program, it doesn't need to define any additional classes itself. However, most programs that you write will be more complex and require you to write other classes, and supporting Java code.

The DateApp application does use two other classes--the System class and the Date class--that are provided with the Java development environment. The System class provides system-independent access to system-dependent functionality. For more information about the System class, see Using System Resources . The Date class provides access to system-indepedent date functionality. It is through the use of these two classes that you'll learn about objects, instance methods and variables, and static methods and variables.

Declaring, Instantiating and Initializing an Object

The first line in the main() method, declares, instantiates and initializes an object named today. The constructor used to initialize today is the default constructor which initializes the new Date object to contain the current day and time.

Static vs. Instance

The second line of the main() method uses dot notation to refer to a class's or object's variables or methods. This one line of code illustrates the use of both static methods and variables and instance variables and methods.


Previous | Next | Trail Map | Writing Java Programs | The Anatomy of a Java Application