The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Learning the Java Language
Lesson: Object Basics and Simple Data Objects

Creating Objects

As you know, a class provides the blueprint for objects. Therefore, to create an object, you must have a class of the appropriate type. Each of the following statements taken from the CreateObjectDemo(in a .java source file) program creates an object:
Point origin_one = new Point(23, 94);
Rectangle rect_one = new Rectangle(origin_one, 100, 200);
Rectangle rect_two = new Rectangle(50, 100);
The first line creates an object from the Point(in a .java source file) class and the second and third lines each create an object from the Rectangle(in a .java source file) class.

Each statement has three parts:

  1. Declaration: Point origin_one, Rectangle rect_one, and Rectangle rect_two are all variable declarations that associate a name with a type. Declaring a variable to refer to an object is not a necessary part of object creation. However, a variable declaration often appears on the same line as the code to create an object so we discuss declaring a variable to refer to an object here.
  2. Instantiation: new is a Java operator that creates the new object (allocates space for it).
  3. Initialization: The new operator is followed by a call to a constructor. For example, Point(23, 94) is a call to Point's only constructor. Constructors initialize the new object.
The next three subsections discuss each of these actions in detail:

Declaring a Variable to Refer to an Object

From the Variables(in the Learning the Java Language trail) section in the previous lesson, you learned that to declare a variable, you write:
type name
This notifies the compiler that you will use name to refer to data whose type is type.

In addition to the primitive types, such as int and boolean, provided directly by the Java platform, classes and interfaces are also types. So to declare a variable to refer to an object, you can use the name of a class or an interface, as the variable's type. The sample program uses both the Point and the Rectangle class names as types to declare variables.

Point origin_one = new Point(23, 94);
Rectangle rect_one = new Rectangle(origin_one, 100, 200);
Rectangle rect_two = new Rectangle(50, 100);
Declarations do not create new objects or allocate any space. So Rectangle rect_one does not create a new rectangle object. It just declares a variable named rect_one that will be used to refer a Rectangle object. Thus the following code will result in a runtime error:
Rectangle rect_one;	// OK. Declare a variable, but don't create an object.
rect_one.width = 25;	// ERROR. rect_one is null -- there is no rectangle object.
To create an object you must instantiate it with the new operator.

Instantiating an Object

The new operator instantiates a class by allocating memory for a new object. new requires a single, postfix argument: a call to a constructor. [PENDING: check the following] The constructor fills two roles. First, it provides the name of the class. new uses the class name to determine the total amount of memory to allocate and the variables to allocate within the new object. Second, new calls the constructor to initialize the new object.

[PENDING: do a for example with Point, and draw a picture]

The new operator returns a reference to the new object. This reference should either be used directly or assigned to a variable. Otherwise, the object will have been created for nothing. In our example, the references returned by new are assigned to variables of the appropriate type.

Initializing an Object

Here's the code for the Point class:
public class Point {
    public int x = 0;
    public int y = 0;
    // a constructor!
    public Point(int x, int y) {
	this.x = x;
	this.y = y;
    }
}
This class contains a single constructor. You can recognize a class's constructors because they have the same name as the class and have no return type. This is the constructor used to initialize the Point object called origin_one in the CreateObjectDemo program.
Point origin_one = new Point(23, 94);
The constructor takes two integer arguments and the previous statement provides 23 and 94 as values for those arguments.

Here's the code for the Rectangle class, which contains four constructors:

public class Rectangle {
    public int width = 0;
    public int height = 0;
    public Point origin;

    // four constructors
    public Rectangle() {
	origin = new Point(0, 0);
    }
    public Rectangle(Point p) {
	origin = p;
    }
    public Rectangle(int w, int h) {
	this(new Point(0, 0), w, h);
    }
    public Rectangle(Point p, int w, int h) {
	origin = p;
	width = w;
	height = h;
    }

    // a method for moving the rectangle
    public void move(int x, int y) {
	origin.x = x;
	origin.y = y;
    }

    // a method for computing the area of the rectangle
    public int area() {
	return width * height;
    }
}
Each constructor lets you provide initial values for different aspects of the rectangle: the origin, the width and height, all three, or none. If a class has multiple constructors, they all have the same name but a different number of arguments or different typed arguments. The compiler differentiates the constructors, and knows which one to call, depending on the arguments. So when the compiler encounters the following code, it knows to call the constructor that requires three arguments:
Rectangle rect_one = new Rectangle(origin_one, 100, 200);
This call initializes the rectangle's origin to the POint object called origin_one, its width to 100, and its height to 200. When the compiler encounters the next line of code, it knows to call the constructor that requires two integer arguments, which provide the initial values for the width and height:
Rectangle rect_two = new Rectangle(50, 100);
The Rectangle constructor used below doesn't take any arguments:
Rectangle rect = new Rectangle();
A constructor that takes no arguments, such as the one shown, is called a no-argument constructor. If a class does not explicitly declare any constructors, Java automatically provides a no-argument constructor, called the default constructor, that does nothing. Thus all classes have at least one constructor.

This section talked about how to use a constructor. Providing Constructors for Your Classes(in the Learning the Java Language trail) explains how to write constructors for your classes.


Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form