Creating Objects (The Java™ Tutorials - Learning The Java Language - Classes and Objects)
Creating Objects (The Java™ Tutorials - Learning The Java Language - Classes and Objects)
Search
The Java™ Tutorials Hide TOC
The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory. The new operator also invokes the object constructor.
Note: The phrase "instantiating a class" means the same thing as "creating an object." When you create an object, you are creating an "instance" of a class, therefore "instantiating" a
class.
The new 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 new operator returns a reference to the object it created. This reference is usually assigned to a variable of the appropriate type, like:
The reference returned by the new operator does not have to be assigned to a variable. It can also be used directly in an expression. For example:
Initializing an Object
This class contains a single constructor. You can recognize a constructor because its declaration uses the same name as the class and it has no return type. The constructor in the Point class
takes two integer arguments, as declared by the code (int a, int b). The following statement provides 23 and 94 as values for those arguments:
The result of executing this statement can be illustrated in the next figure:
Here's the code for the Rectangle class, which contains four constructors:
// four constructors
public Rectangle() {
origin = new Point(0, 0);
}
public Rectangle(Point p) {
origin = p;
}
public Rectangle(int w, int h) {
origin = new Point(0, 0);
width = w;
height = h;
}
public Rectangle(Point p, int w, int h) {
origin = p;
width = w;
height = h;
}
Each constructor lets you provide initial values for the rectangle's origin, width, and height, using both primitive and reference types. If a class has multiple constructors, they must have different
signatures. The Java compiler differentiates the constructors based on the number and the type of the arguments. When the Java compiler encounters the following code, it knows to call the
constructor in the Rectangle class that requires a Point argument followed by two integer arguments:
This calls one of Rectangle's constructors that initializes origin to originOne. Also, the constructor sets width to 100 and height to 200. Now there are two references to the same
Point object—an object can have multiple references to it, as shown in the next figure:
The following line of code calls the Rectangle constructor that requires two integer arguments, which provide the initial values for width and height. If you inspect the code within the constructor,
you will see that it creates a new Point object whose x and y values are initialized to 0:
The Rectangle constructor used in the following statement doesn't take any arguments, so it's called a no-argument constructor:
All classes have at least one constructor. If a class does not explicitly declare any, the Java compiler automatically provides a no-argument constructor, called the default constructor. This default
constructor calls the class parent's no-argument constructor, or the Object constructor if the class has no other parent. If the parent has no constructor (Object does have one), the compiler
will reject the program.
About Oracle | Contact Us | Legal Notices | Terms of Use | Your Privacy Rights
Copyright © 1995, 2022 Oracle and/or its affiliates. All rights reserved.
Préférences en matière de cookies | Ad Choices