A class definition normally includes one or more initializer functions, also known as constructors. These constitute a special group of functions that are called when creating objects of the enclosing class. The main purpose of an initializer function is to set the object created to an appropriate (initial) state. It carries this out by assigning initial values to the attributes of the object.
The following example includes the declaration of two attributes, age and obj_name of a class named Person.
class Person is private variables // variable data declarations integer age string obj_name
Note | If no initializer function is defined in the class, the default values set by the compiler are used for all the attributes. |
If no initializer function is included in the class definition, the default values are set by the Java compiler (zero and empty) for the two attributes.
It is good programming practice to define at least a default initializer function. For example, in class Person the default values for the two attributes are set to 21 for age and "None" for obj_name. This function is defined as:
description This is the default initializer method. */ function initializer is begin set age = 21 set obj_name = "None" endfun initializer
Recall that the general structure of the statement to create an object is:
create 〈 object_ref_name 〉 of class 〈 class_name 〉
This statement implicitly uses the default initializer function in the class. A complete initializer function sets the value of all the attributes to the values given when called. These values are given as arguments in the statement that creates the object. The general statement to create an object with given values for the attributes is:
create 〈 object_ref_name 〉 of class 〈 class_name 〉 using 〈 argument_list 〉
A second initializer function is included in class Person to set given initial values to the attributes of the object when created. For example, suppose there is an object reference person_obj of class Person declared, to create an object referenced by person_obj with initial value of 32 for age and " J. K. Hunt" for the obj_name:
create person_obj of class Person using 32, "J. K. Hunt"
The definition of this complete initializer function in class Person includes parameter definitions for each attribute:
description This is a complete initializer, it sets the attributes to the values given when an object is created. */ function initializer parameters integer iage, string iname is begin set age = iage set obj_name = iname endfun initializer
Note | The definition of two or more functions with the same name is known as overloading. This facility of the programming language allows any function in a class to be overloaded. In other words, it is a redefinition of a function with another function with the same name, but with a different number of and types of parameters. |
It is very useful in a class to define more than one initializer function. This way, there is more than one way to initialize an object of the class. When a class defines two initializer functions, the compiler differentiates them by the number of and the type of parameters.