Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
As you know, a class provides the blueprint for objects; you create an object from a class. Each of the following statements taken from theCreateObjectDemo
program creates an object:The first line creates an object from thePoint origin_one = new Point(23, 94); Rectangle rect_one = new Rectangle(origin_one, 100, 200); Rectangle rect_two = new Rectangle(50, 100);Point
class and the second and third lines each create an object from theRectangle
class.Each statement has three parts:
The next three subsections discuss each of these actions in detail:
- Declaration: The code set in red in the previous listing are all variable declarations that associate a name with a type. When you create an object, you do not have to declare a variable to refer to it. However, a variable declaration often appears on the same line as the code to create an object.
- Instantiation:
new
is a Java operator that creates the new object (allocates space for it).- Initialization: The
new
operator is followed by a call to a constructor. For example,Point(23, 94)
is a call toPoint
's only constructor. The constructor initializes the new object.
From the Variables section in the previous lesson, you learned that to declare a variable, you write:This notifies the compiler that you will use name to refer to data whose type is type.type nameIn addition to the primitive types, such as
int
andboolean
, 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 thePoint
and theRectangle
class names as types to declare variables.Declarations do not create new objects. The codePoint origin_one = new Point(23, 94); Rectangle rect_one = new Rectangle(origin_one, 100, 200); Rectangle rect_two = new Rectangle(50, 100);Point origin_one
does not create a newPoint object
; it just declares a variable, namedorigin_one
, that will be used to refer to aPoint
object. The reference is empty until assigned, as illustrated in the next figure. An empty reference is known as a null reference.To create an object you must instantiate it with the new
operator.
Thenew
operator instantiates a class by allocating memory for a new object. Thenew
operator requires a single, postfix argument: a call to a constructor. The name of the constructor provides the name of the class to instantiate. The constructor initializes the new object.The
new
operator returns a reference to the object it created. Often, this reference is assigned to a variable of the appropriate type. If the reference is not assigned to a variable, the object is unreachable after the statement in which thenew
operator appears finishes executing.Initializing an Object
Here's the code for thePoint
class:This class contains a single constructor. You can recognize a constructor because it has the same name as the class and has no return type. The constructor in thepublic class Point { public int x = 0; public int y = 0; //A constructor! public Point(int x, int y) { this.x = x; this.y = y; } }Point
class takes two integer arguments, as declared by the code(int x, int y)
. The following statement provides 23 and 94 as values for those arguments:The effect of the previous line of code can be illustrated in the next figure:Point origin_one = new Point(23, 94);Here's the code for the Rectangle
class, which contains four constructors:Each constructor lets you provide initial values for different aspects of the rectangle: the origin; the width, and the 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 Java platform differentiates the constructors, based on the number and the type of the arguments. When the Java platform encounters the following code, it knows to call the constructor in thepublic 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; } }Rectangle
class that requires a Point argument followed by two integer arguments:This call initializes the rectangle'sRectangle rect_one = new Rectangle(origin_one, 100, 200);origin
variable to thePoint
object referred to byorigin_one
. The code also setswidth
to 100 andheight
to 200. Now there are two references to the samePoint
object; an object can have multiple references to it, as shown in the next figure:Multiple references can refer to the same object. The following line of code calls the constructor that requires two integer arguments, which provide the initial values for width
andheight
. If you inspect the code within the constructor, you will see that it creates a newPoint
object whosex
andy
values are initialized to 0:TheRectangle rect_two = new Rectangle(50, 100);Rectangle
constructor used in the following statement doesn't take any arguments, so it's called a no-argument constructor:If a class does not explicitly declare any constructors, the Java platform automatically provides a no-argument constructor, called the default constructor, that does nothing. Thus, all classes have at least one constructor.Rectangle rect = new Rectangle();
This section talked about how to use a constructor.
Providing Constructors for Your Classes explains how to write constructors for your classes.
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2002 Sun Microsystems, Inc. All rights reserved.