A class describes a generic template for creating, or instantiating, objects. In fact, an object must be instantiated before it can be used. To understand how to instantiate an object of a class and how to call methods of the class, you must know the API of a class, which the creators of the class make public. For example here.
Instantiating an object consists of defining an object reference—which will hold the address of the object in memory—and calling a special method of the class called a constructor, which has the same name as the class. The job of the constructor is to assign initial values to the data of the class.
The example below illustrates how to instantiate objects of the EndOfTimes class. Note that we store the EndOfTimes.java file in the same folder as Constructors.java.
Notice that EndOfTimes is underlined red. That’s because it is not actually a known method of an API. The year 4.1 billion is also underlined red because it is sufficiently large to be out of range.
Declaring an object reference is very much like declaring a variable of a primitive type; you specify the data type and an identifier. For example, to declare an integer variable named number1, you provide the data type (int) and the identifier (number1), as follows:
One notable difference in declaring an object reference is that its data type is a class, not a primitive data type. Here is the syntax for declaring an object reference:
In the example above, lines 9, 12, and 14 declare object references for a EndOfTimes object. EndOfTimes, the class name, is the data type, and maitreya, kaliYuga, and ragnarok are the object references. Object references can refer to any object of its class. For example, EndOfTimes object references can point to any EndOfTimes object, but an EndOfTimes object reference cannot point to objects of other classes, such as a JapaneseStudent object. Once an object reference has been declared, you instantiate the object using the following syntax:
This calls a constructor of the class to initialize the data. The argument list consists of a comma-separated list of initial data values to assign to the object. Classes often provide multiple constructors with different argument lists. Depending on which constructor you call, you can accept default values for the data or specify initial values for the data. When you instantiate an object, your argument list—that is, the number of arguments and their data types—must match one of the constructors’ argument lists.
The EndOfTimes class has a constructor. The constructor, EndOfTimes( ), is called the default constructor, because its argument list is empty. This constructor assigns default values to all data in the object. Thus, in line 14, which uses the default constructor, the data for the ragnarok object is set to the default values for the EndOfTimes class, whichever those may have been. Remember that API’s are designed by authors previously and you can then find them on websites such as the one linked above.
We also see another constructor for the EndOfTimes class, EndOfTimes( int mm, int dd, int yy ), takes three arguments, all of which should evaluate to integer values. The first argument is the value for the month, the second argument is the value for the day, and the third argument is the value for the year. Lines 10 and 12 of instantiate EndOfTimes objects using the second constructor. In line 10, the argument list tells the constructor to give the value 3 to the month, 26 to the day, and 2300 to the year. In line 12, the argument list tells the constructor to give the value 11 to the month, 4 to the day, and 4100000000 to the year (which is out of range).
Note that no data types are given in the argument list, only the initial values for the data. The data types of the arguments are specified in the API so that the client of the class knows what data types the constructor is expecting for its arguments. Lines 12 and 14 also illustrate that you can combine the declaration of the object reference and instantiation of the object in a single statement. When an object is instantiated, the JVM allocates memory to the new object. The object reference is assigned an address that the JVM uses to find that object in memory. The figure below shows one of the three objects instantiated in code above.
It’s important to understand that an object reference and the object data are different: The object reference represents a memory location. Notice that the object references, maitreya, kaliYuga, and ragnarok, point to the locations of the object data.