Objects and Classes in Java |
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 newRectangle
object from theRectangle
class given in the previous section:This single statement performs three actions:Rectangle rect = new Rectangle();
- Declaration:
Rectangle rect
is a variable declaration that declares to the compiler that the namerect
will be used to refer to aRectangle
object. Notice that a class name is used as the variable's type.- Instantiation:
new
is a Java operator that creates the new object (allocates space for it).- Initialization:
Rectangle()
is a call toRectangle
'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:Variables and Data Types 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:Rectangle rect;In Java, classes and interfaces can be used as data types. Sotype nametype
can be the name of a class such as theRectangle
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 newRectangle
object, just a variable namedrect
to hold aRectangle
object. To create aRectangle
object, or any other object, use thenew
operator.Instantiating an Object
Thenew
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. Thenew
operator creates the object, and the constructor initializes it. Here's an example of using thenew
operator to create aRectangle
object:Here,new Rectangle(100, 200);Rectangle(100, 200)
is the argument tonew
. Thenew
operator returns a reference to the newly created object. This reference can be assigned to a variable of the appropriate type, as shown here.After this statement,Rectangle rect = new Rectangle(100, 200);rect
refers to aRectangle
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 forRectangle
's constructors: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):public Rectangle(Point p) public Rectangle(int w, int h) public Rectangle(Point p, int w, int h) public Rectangle()And when the compiler encounters the next line of code, it knows to call the constructor that requires aRectangle rect = new Rectangle(100, 200);Point
(which provides the initial values for the origin of the newRectangle
):TheRectangle rect = new Rectangle(new Point(44,78));Rectangle
constructor used below doesn't take any arguments:A constructor that takes no arguments, such as the one shown, is the default constructor. If a class (like theRectangle rect = new Rectangle();SimplePoint
andSimpleRectangle
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.
Objects and Classes in Java |