15
| member assignment list
| This constructor contains an example of a situation where initialization of the data members requires a member initialization
list; i.e., initialization cannot be achieve by performing assignments in the body of the constructor. As there is no
default constructor for the Name class , creating a name object requires that information (last name. first name)
be supplied at the time of creation. Declaring an Employee object results in the creation of that object,
and since it has a data member of type Name , it must be immediately supplied with its constructor information.
This cannot wait until the constructor body, as inside that body one might begin using the name data member
before it's been initialized; i.e., by the time we reach the '{' of the constructor body all data members
must be initialized, and thus we need to use the member initialization list to initialize the data member prior to execution
of the body (the same holds true for the salaryInfo data member.
| Java's instance variables are references, not objects, and are initialized to null . Accessing them prior to
creating objects that they reference, results in an error (which is safer than working with an uninitialized value).
Java does have a situation in which initialization of certain instance variable must be performed before anything else is done
iin the constructor; rather than having a member initialization list, Java requires the initialization code (which is easily
recognized by the compiler) must be the first code in the constructor.
|