Now you know, as the objects are created, they must be given initial values. Earlier, we have done this by using two approaches: the first approach uses the dot operator to access the instance variables and then assigns values to them individually as shown in the statement given below: rect1.length = 15; rect1.width = 10; The second approach takes the help of a method like setData( ) to initialize each object individually using statements like: rect1.setData(15, 10); There is a third approach also to initialize the instance variables by using constructors. Java supports a special type of member function or method called as constructor, which enables an object to initialize itself when it is created. This is known as automatic initialization of objects. A constructor is a special member function, whose task is to initialize the objects of its class. It’s name is same as the class name. The constructor is invoked whenever an object of its associated class is created. It is called constructor because it constructs the values of data members of the class. Properties of Constructors
Let us consider the class Rectangle again. We can replace the setData method by a constructor method. Given below is the code of the program as given in the earlier chapter, except that in the program given below, instead of setData( ) method, we have used the constructor method to initialize the instance variables. // Declaration of class Rectangle class Rectangle { // Declaration of variables int length, width; /* instead of setData( ), we have used the constructor method here Rectangle(int x, int y) */ { length = x; width = y; } // Definition of method rectArea int rectArea( ) { int area; area = length * width; return area; } } class RectangleArea { public static void main(String args[]) { // constructor is called here. Rectangle rect1 = new Rectangle(15,10); int area1 = rect1.rectArea( ); System.out.println(" Area1 = " + area1); } } Output: Area1 = 150 The statement Rectangle rect1 = new Rectangle(15, 10) calls the constructor of the class Rectangle. 15 and 10 are passed as parameters, which are then assigned to the instance variables length and width. Rectangle(int x, int y) { length=x; width=y; } There is no need to write any statement to invoke the constructor function as we do with normal member method. Like we used, setData() method to assign values to the instance variables length and width in this program in previous session of this chapter, then every time we had to invoke setData() method for each of the objects separately. This would be very inconvenient, if there are large number of objects. Multiple Constructors in a Class The above program has been modified to show how more than one constructor can be defined in a class. When more than one constructor function is defined in a class, we say that the constructor is overloaded. // Declaration of class Rectangle class Rectangle { // Declaration of variables int length, width; // constructor1, no parameters are passed to this constructor Rectangle( ) { /* length and width instance variables are initialized to 0 when constructor1 is called */ length = 0; width=0; } // constructor2, two integer parameters are passed Rectangle(int x, int y) { /* the parameters passed are assigned to length and width instance variables */ length = x; width = y; } // Definition of the method rectArea int rectArea( ) { int area; area = length * width; return area; } } class RectangleArea { public static void main(String args[]) { // for rect1 constructor1 is invoked Rectangle rect1 = new Rectangle( ); rect1.length = 15; rect1.width = 10; int area1 = rect1.rectArea( ); System.out.println(" Area1 = " + area1); // for rect2 constructor2 in invoked Rectangle rect2 = new Rectangle(20, 12); int area2 = rect2.rectArea(); System.out.println("Area2 = " + area2); } } Output: Area1 = 150 Area2 = 240 Two constructors have been defined for class Rectangle. These are constructor1 and constructor2. // Declaration of class Rectangle class Rectangle { // Declaration of variables int length, width; // constructor1, no parameters are passed to this constructor Rectangle( ) { /* length and width instance variables are initialized to 0 when constructor1 is called */ length = 0; width=0; } // constructor2, two integer parameters are passed Rectangle(int x, int y) { /* the parameters passed are assigned to length and width instance variables */ length = x; width = y; } } When object rect1 is created, no parameters are passed to the constructor1, and the instance variables length and width of rect1 are initialized to 0. // for rect1 constructor1 is invoked Rectangle rect1 = new Rectangle( ); When object rect2 is created, values 20 and 12 are passed as parameters to constructor2, and the instance variables length and width of rect2 are assigned these values respectively. // for rect2 constructor2 in invoked Rectangle rect2=new Rectangle(20,12); |
Course Content > Session 5 >