Previous | Next | Trail Map | Writing Java Programs | Java Objects


Creating an Object

In Java, you create an object by creating an instance of a class or, in other words, instantiating a class. To create a new object, use Java's new operator plus a constructor for the type of object that you want to create.
new Constructor(...)
Constructors are special methods provided by every Java class allowing programmers to create and initialize objects of that type. If you are creating your own class and want more information about how to write constructors (rather than how to use them which is what we cover in this section), refer to Missing Page.

Constructors have the same name as the class and, because Java supports method name overloading, a class can have any number of constructors. The constructors are differentiated from one another by the number and type of their arguments. For example, each of the following methods is a constructor for the class Rectangle in the java.awt package:

public Rectangle() {
    . . .
}
public Rectangle(int height, int width) {
    . . .
}
public Rectangle(int x, int y, int height, int width) {
    . . .
}
Typically, a constructor uses its arguments to initialize the new object's state. So, the first constructor shown above initializes a new Rectangle to some reasonable default, the second constructor initializes the new Rectangle with the specified width and height, and so on. When creating an object, you would choose the constructor whose arguments best reflected how you wanted to initialize the new object.

Suppose that you were writing a drawing application and the user drew rectangle on the screen positioned at the origin that was 100 pixels wide by 50 pixels high. Your program would want to create an object in memory that represented this rectangle so that the user could continue to manipulate the rectangle. If you were using the Rectangle class from java.awt, you would first look at the list of constructors supported by the Rectangle class and choose the one that best suited your needs. Since you know the dimensions and position of the Rectangle in advance, it's probably most convenient to use the following Rectangle constructor which allows you to initialize the Rectangle with an x, y origin and a width and height directly from integer values.

public Rectangle(int x, int y, int width, int height)
Thus, you would create the rectangle with this Java statement:
new Rectangle(0, 0, 100, 50);
This statement creates a new object of the type Rectangle (allocates ample memory for it on the heap), does some primitive initialization (zeros out all of the object's variables), and then calls the indicated constructor to perform more personalized initialization on the object (initialize the origin and dimenstions to those specified in the constructor's arguments).

The new operator returns a reference to the newly created object. Typically, you will want to assign the return value of the new operator to a variable so that you can use the object later. The variable must be of the same type as the object or one of its superclasses.

Rectangle rect = new Rectangle(0, 0, 100, 50);


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