|
|
Start of Tutorial > Start of Trail > Start of Lesson | Search |
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 newRectangleobject from theRectangleclass given in the previous section:This single statement performs three actions:Rectangle rect = new Rectangle();
- Declaration:
Rectangle rectis a variable declaration that declares to the compiler that the namerectwill be used to refer to aRectangleobject. Notice that a class name is used as the variable's type.- Instantiation:
newis a Java operator that creates the new object (allocates space for it).- Initialization:
Rectangle()is a call toRectangle's constructor, which initializes the 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 TypesRectangle rect;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:
In Java, classes and interfaces can be used as data types. Sotype nametypecan be the name of a class such as theRectangleclass 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 rectdoes not create a newRectangleobject, just a variable namedrectto hold aRectangleobject. To create aRectangleobject, or any other object, use thenewoperator.
Thenewoperator instantiates a class by allocating memory for a new object of that type.newrequires 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. Thenewoperator creates the object, and the constructor initializes it. Here's an example of using thenewoperator to create aRectangleobject:Here,new Rectangle(100, 200);Rectangle(100, 200)is the argument tonew. Thenewoperator 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);rectrefers to aRectangleobject whose origin is at (0, 0), width is 100, and height is 200.
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));Rectangleconstructor 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 (like theRectangle rect = new Rectangle();SimplePointandSimpleRectangleclasses shown at the very beginning of this lesson) does not explicitly define any constructors at all, Java automatically provides a no-argument constructor, called the default 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.
|
|
Start of Tutorial > Start of Trail > Start of Lesson | Search |