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();Date
object (Date
is a class in thejava.util
package). This single statement actually performs three actions: declaration, instantiation, and initialization.Date today
is a variable declaration which simply declares to the compiler that the nametoday
will be used to refer to an object whose type isDate
, thenew
operator instantiates theDate
class (thereby creating a newDate
object), andDate
initializes the object.Declaring an Object
While 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 nameDate
class 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 today
does not create a newDate
object, just a variable namedtoday
to hold aDate
object. To instantiate theDate
class, or any other class, 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 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. Thenew
operator creates the object, the constructor initializes it.Here's an example of using the
new
operator to create aRectangle
object (Rectangle
is a class in thejava.awt
package):In the example,new Rectangle(0, 0, 100, 200);Rectangle(0, 0, 100, 200)
is a call to a constructor for theRectangle
class.The
new
operator 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);Rectangle
can be used as a data type in your Java programs. The value of any variable whose data type is a reference type, such asrect
, 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 Object
As 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 theDate
object used at the beginning of this section. TheDate
constructor 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
Date
object earlier, theDate
class provides another constructor that initializes the newDate
with 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 |