|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 new Date object (Date is a class in the java.util package). This single statement actually performs three actions: declaration, instantiation, and initialization.Date today = new Date();
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 Date, the
newoperator instantiates the Date class (thereby creating a new Data object), and
Date()initializes 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 the Date class or the name of an interface. Variables and Data Types talks more about variable declarations.type name
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 Date object, just a variable named
todayto hold a Date object. To instantiate the Date class, 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 Rectangle object (Rectangle is a class in the java.awt package):In the example,new Rectangle(0, 0, 100, 200);
Rectangle(0, 0, 100, 200)is a call to a constructor for the Rectangle class.
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 can be used as a data type in your Java programs. The value of any variable whose data type is a reference type, such asRectangle rect = new Rectangle(0, 0, 100, 200);
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 Date object used at the beginning of this section. The Date 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. Like Date, most classes have at least one constructor, the default constructor.Date()
If a class has multiple constructors, they all have the same name but they will 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, the Date class provides another constructor that initializes the new Date with a year, month, and day:The compiler can differentiate the constructors through the type and number of the arguments.Date MyBirthday = new Date(1963, 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|