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


Using Objects in Java Programs

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 class methods and variables.
Note: You may have noticed that this program imported the Date class but not the System class. This is because the System class is a member of the java.lang package which is imported for you by default.

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.

Class Members vs. Instance Members

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 class methods and variables and instance variables and methods.

See Also

Object-Oriented Programming Concepts: A Primer
Objects, Classes, and Interfaces


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