Default Constructor Overloading Constructors Using Constructors

J.E.D.I

10.7 Declaring Constructors

We have discussed before the concept of constructors. Constructors are important in instantiating an object. It is a method where all the initializations are placed. The following are the properties of a constructor: 1. Constructors have the same name as the class 2. A constructor is just like an ordinary method, however only the following information can be placed in the header of the constructor, scope or accessibility identifier like public..., constructors name and parameters if it has any. 3. Constructors does not have any return value 4. You cannot call a constructor directly, it can only be called by using the new operator during class instantiation. To declare a constructor, we write, modifier className parameter { statement }

10.7.1 Default Constructor

Every class has a default constructor. The default constructor is the constructor without any parameters. If the class does not specify any constructors, then an implicit default constructor is created. For example, in our StudentRecord class, the default constructor would look like this, public StudentRecord { some code here }

10.7.2 Overloading Constructors

As we have mentioned, constructors can also be overloaded, for example, we have here four overloaded constructors, public StudentRecord{ some initialization code here } public StudentRecordString temp{ this.name = temp; } public StudentRecordString name, String address{ this.name = name; this.address = address; } public StudentRecorddouble mGrade, double eGrade, double sGrade{ mathGrade = mGrade; englishGrade = eGrade; scienceGrade = sGrade; } Introduction to Programming I 161 J.E.D.I

10.7.3 Using Constructors

To use these constructors, we have the following code, public static void main String[] args { create three objects for Student record StudentRecord annaRecord=new StudentRecordAnna; StudentRecord beahRecord=new StudentRecordBeah, Philippines; StudentRecord crisRecord=new StudentRecord80,90,100; some code here } Now, before we move on, let us go back to the static variable studentCount we have declared a while ago. The purpose of the studentCount is to count the number of objects that are instantiated with the class StudentRecord. So, what we want to do here is, everytime an object of class StudentRecord is instantiated, we increment the value of studentCount. A good location to modify and increment the value of studentCount is in the constructors, because it is always called everytime an object is instantiated. For example, public StudentRecord{ some initialization code here studentCount++; add a student } public StudentRecordString temp{ this.name = temp; studentCount++; add a student } public StudentRecordString name, String address{ this.name = name; this.address = address; studentCount++; add a student } public StudentRecorddouble mGrade, double eGrade, double sGrade{ mathGrade = mGrade; englishGrade = eGrade; scienceGrade = sGrade; studentCount++; add a student } Introduction to Programming I 162 J.E.D.I

10.7.4 The this Constructor Call