Previous | Next | Trail Map | Learning the Java Language | Objects and Classes in Java

Creating Objects

In Java, you create an object by creating an instance of a class or, in other words, instantiating a class. Often, you will see a Java object created with a statement like the following, which creates a new Rectangle object from the Rectangle class given in the previous section:
Rectangle rect = new Rectangle();
This single statement performs three actions:
  1. Declaration: Rectangle rect is a variable declaration that declares to the compiler that the name rect will be used to refer to a Rectangle object. Notice that a class name is used as the variable's type.
  2. Instantiation: new is a Java operator that creates the new object (allocates space for it).
  3. Initialization: Rectangle() is a call to Rectangle's constructor, which initializes the object.

Declaring an Object

The declaration of an object is not a necessary part of object creation, although it often appears on the same line. Like other variable declarations, object declarations can also appear alone, like this:
Rectangle rect;
Variables and Data Types(in the Learning the Java Language trail) in the previous lesson discussed variable declarations in detail. To declare an object, you just follow the same rules and declare a variable to refer to that object by declaring its type and name:
type name
In Java, classes and interfaces can be used as data types. So type can be the name of a class such as the Rectangle class or the name of an interface. Classes and interfaces are both reference types (the variable's actual value is a reference to the 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 to which the reference refers.

Declarations notify the compiler that you will use name to refer to a variable whose type is type. Declarations do not create new objects. Rectangle rect does not create a new Rectangle object, just a variable named rect to hold a Rectangle object. To create a Rectangle object, or any other object, 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, postfix argument: a call to a constructor. Each Java class provides a set of constructors used to initialize new objects of that type. The new operator creates the object, and the constructor initializes it. Here's an example of using the new operator to create a Rectangle object:
new Rectangle(100, 200);
Here, Rectangle(100, 200) is the argument to new. The new operator returns a reference to the newly created object. This reference can be assigned to a variable of the appropriate type, as shown here.
Rectangle rect = new Rectangle(100, 200);
After this statement, rect refers to a Rectangle object whose origin is at (0, 0), width is 100, and height is 200.

Initializing an Object

As shown in the code for the Rectangle class, classes can provide one or more constructors to initialize a new object of that type. You can recognize a class's constructors because they have the same name as the class and have no return type. Here are the declarations for Rectangle's constructors:
public Rectangle(Point p)
public Rectangle(int w, int h)
public Rectangle(Point p, int w, int h)
public Rectangle()
Each of these constructors 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 two integer arguments (which initializes the width and height of the new rectangle to the values provided by the arguments):
Rectangle rect = new Rectangle(100, 200);
And when the compiler encounters the next line of code, it knows to call the constructor that requires a Point (which provides the initial values for the origin of the new Rectangle):
Rectangle rect = new Rectangle(new Point(44,78));
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 the default constructor. If a class (like the SimplePoint and SimpleRectangle classes shown at the very beginning of this lesson) does not explicitly define any constructors at all, Java automatically provides a no-argument 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 later in this lesson explains how to write constructors for your classes.


Previous | Next | Trail Map | Learning the Java Language | Objects and Classes in Java