Previous | Next | Trail Map | Writing Java Programs | Objects, Classes, and Interfaces


Creating Objects

In Java, you create an object by creating an instance of a class or, in other words, instantiating a class. You will learn how to create a class later in Creating Classes. Until then, the examples contained herein create objects from classes that already exist in the Java environment.

Often, you will see a Java object created with a statement like this one:

Date today = new Date();
This statement creates a new Date object (Date is a class in the java.util package). This single statement actually performs three actions: declaration, instantiation, and initialization. Date today is a variable declaration which simply declares to the compiler that the name today will be used to refer to an object whose type is Date, the new operator instantiates the Date class (thereby creating a new Data object), and Date initializes the object.

Declaring an Object

While the declaration of an object is not a necessary part of object creation, object declarations often appear on the same line as the creation of an object. Like other variable declarations, object declarations can appear alone like this:
Date today;
Either way, declaring a variable to hold an object is just like declaring a variable to hold a value of primitive type:
type name
where type is the data type of the object and name is the name to be used for the object. In Java, classes and interfaces are just like a data type. So type can be the name of a class such as the Date class or the name of an interface. Variables and Data Types(in the Writing Java Programs trail) discusses variable declarations in detail.

Declarations notify the compiler that you will be using name to refer to a variable whose type is type. Declarations do not create new objects. Date today does not create a new Date object, just a variable named today to hold a Date object. To instantiate the Date class, or any other class, use the new operator.

Instantiating an Object

The new operator instantiates a class by allocating memory for a new object of that type. new requires a single argument: a call to a constructor method. Constructor methods are special methods provided by each Java class that are responsible for initializing new objects of that type. The new operator creates the object, the constructor initializes it.

Here's an example of using the new operator to create a Rectangle object (Rectangle is a class in the java.awt package):

new Rectangle(0, 0, 100, 200);
In the example, Rectangle(0, 0, 100, 200) is a call to a constructor for the Rectangle class.

The new operator returns a reference to the newly created object. This reference can be assigned to a variable of the appropriate type.

Rectangle rect = new Rectangle(0, 0, 100, 200);
(Recall from Variables and Data Types(in the Writing Java Programs trail) that a class essentially defines a new reference data type. So, Rectangle can be used as a data type in your Java programs. The value of any variable whose data type is a reference type, such as rect, is a reference (a pointer in other terminology) to the actual value or set of values represented by the variable. In this tutorial, a reference may also be called an object reference or an array reference depending on the data that the reference is referring to.)

Initializing an Object

As mentioned previously, classes provide constructor methods to initialize a new object of that type. A class may provide multiple constructors to perform different kinds of initialization on new objects. When looking at the implementation for a class, you can recognize the constructors because they have the same name as the class and have no return type. Recall the creation of the Date object used at the beginning of this section. The Date constructor used there doesn't take any arguments:
Date()
A constructor that takes no arguments, such as the one shown, is known as the default constructor. Like Date, most classes have at least one constructor, the default constructor.

If a class has multiple constructors, they all have the same name but a different number or type of arguments. Each constructor initializes the new object in a different way. Besides the default constructor used to initialize a new Date object earlier, the Date class provides another constructor that initializes the new Date with a year, month, and day:

Date MyBirthday = new Date(1996, 8, 30);
The compiler can differentiate the constructors through the type and number of the arguments.

This section talked about how to use a constructor. Constructors later in this lesson explains how to write constructor methods for your classes.


Previous | Next | Trail Map | Writing Java Programs | Objects, Classes, and Interfaces