|Objects, Classes, and Interfaces|
In Java, you create an object by creating an instance of a class or, in other words, instantiating a class. You will learn how to create a class later in Creating Classes. Until then, the examples contained herein create objects from classes that already exist in the Java environment.
Often, you will see a Java object created with a statement like this one:This statement creates a newDate today = new Date();
Dateis a class in the
java.utilpackage). This single statement actually performs three actions: declaration, instantiation, and initialization.
Date todayis a variable declaration which simply declares to the compiler that the name
todaywill be used to refer to an object whose type is
newoperator instantiates the
Dateclass (thereby creating a new
Dateinitializes the object.
Declaring an ObjectWhile the declaration of an object is not a necessary part of object creation, object declarations often appear on the same line as the creation of an object. Like other variable declarations, object declarations can appear alone like this:Either way, declaring a variable to hold an object is just like declaring a variable to hold a value of primitive type:Date today;where type is the data type of the object and name is the name to be used for the object. In Java, classes and interfaces are just like a data type. So type can be the name of a class such as thetype name
Dateclass or the name of an interface. Variables and Data Types discusses variable declarations in detail.
Declarations notify the compiler that you will be using name to refer to a variable whose type is type. Declarations do not create new objects.
Date todaydoes not create a new
Dateobject, just a variable named
todayto hold a
Dateobject. To instantiate the
Dateclass, or any other class, use the
Instantiating an ObjectThe
newoperator instantiates a class by allocating memory for a new object of that type.
newrequires a single argument: a call to a constructor method. Constructor methods are special methods provided by each Java class that are responsible for initializing new objects of that type. The
newoperator creates the object, the constructor initializes it.
Here's an example of using the
newoperator to create a
Rectangleis a class in the
java.awtpackage):In the example,new Rectangle(0, 0, 100, 200);
Rectangle(0, 0, 100, 200)is a call to a constructor for the
newoperator returns a reference to the newly created object. This reference can be assigned to a variable of the appropriate type.(Recall from Variables and Data Types that a class essentially defines a new reference data type. So,Rectangle rect = new Rectangle(0, 0, 100, 200);
Rectanglecan be used as a data type in your Java programs. The value of any variable whose data type is a reference type, such as
rect, is a reference (a pointer in other terminology) to the actual 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 that the reference is referring to.)
Initializing an ObjectAs mentioned previously, classes provide constructor methods to initialize a new object of that type. A class may provide multiple constructors to perform different kinds of initialization on new objects. When looking at the implementation for a class, you can recognize the constructors because they have the same name as the class and have no return type. Recall the creation of the
Dateobject used at the beginning of this section. The
Dateconstructor used there doesn't take any arguments:A constructor that takes no arguments, such as the one shown, is known as the default constructor. LikeDate()
Date, most classes have at least one constructor, the default constructor.
If a class has multiple constructors, they all have the same name but a different number or type of arguments. Each constructor initializes the new object in a different way. Besides the default constructor used to initialize a new
Dateobject earlier, the
Dateclass provides another constructor that initializes the new
Datewith a year, month, and day:The compiler can differentiate the constructors through the type and number of the arguments.Date MyBirthday = new Date(1996, 8, 30);
This section talked about how to use a constructor. Constructors later in this lesson explains how to write constructor methods for your classes.
|Objects, Classes, and Interfaces|