![]() ![]() ![]() |
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
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 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 the
Rectangle
class.
Each statement has three parts:
The next three subsections discuss each of these actions in detail:
- Declaration:
Point origin_one
,Rectangle rect_one
, andRectangle 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.- 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. Constructors initialize the new object.
From the Variablessection 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 or allocate any space. SoPoint origin_one = new Point(23, 94); Rectangle rect_one = new Rectangle(origin_one, 100, 200); Rectangle rect_two = new Rectangle(50, 100);Rectangle rect_one
does not create a new rectangle object. It just declares a variable namedrect_one
that will be used to refer aRectangle
object. Thus the following code will result in a runtime error:To create an object you must instantiate it with theRectangle 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.new
operator.
Thenew
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 bynew
are assigned to variables of the appropriate type.
Here's the code for thePoint
class: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 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
object calledorigin_one
in theCreateObjectDemo
program.The constructor takes two integer arguments and the previous statement provides 23 and 94 as values for those arguments.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 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: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; } }This call initializes the rectangle's origin to theRectangle rect_one = new Rectangle(origin_one, 100, 200);POint
object calledorigin_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:TheRectangle rect_two = new Rectangle(50, 100);Rectangle
constructor used below doesn't take any arguments: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.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 |