Chapter: Java The Complete Reference - The Java Language - Introducing Classes

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail

Constructors - Java

It can be tedious to initialize all of the variables in a class each time an instance is created. Even when you add convenience functions like setDim( ), it would be simpler and more concise to have all of the setup done at the time the object is first created.

Constructors

 

It can be tedious to initialize all of the variables in a class each time an instance is created. Even when you add convenience functions like setDim( ), it would be simpler and more concise to have all of the setup done at the time the object is first created. Because the requirement for initialization is so common, Java allows objects to initialize themselves when they are created. This automatic initialization is performed through the use of a constructor.

 

A constructor initializes an object immediately upon creation. It has the same name as the class in which it resides and is syntactically similar to a method. Once defined, the constructor is automatically called when the object is created, before the new operator completes. Constructors look a little strange because they have no return type, not even void. This is because the implicit return type of a class’ constructor is the class type itself. It is the constructor’s job to initialize the internal state of an object so that the code creating an instance will have a fully initialized, usable object immediately.

 

You can rework the Box example so that the dimensions of a box are automatically initialized when an object is constructed. To do so, replace setDim( ) with a constructor.

Let’s begin by defining a simple constructor that simply sets the dimensions of each box to the same values. This version is shown here:

 

/*        Here, Box uses a constructor to initialize the dimensions of a box.

 

*/

 

class Box { double width; double height; double depth;

 

    This is the constructor for Box. Box() {

 

System.out.println("Constructing Box"); width = 10;

 

height = 10; depth = 10;

 

}

 

     compute and return volume

 

double volume() {

 

return width * height * depth;

 

}

 

}

 

class BoxDemo6 {

 

public static void main(String args[]) {

 

// declare, allocate, and initialize Box objects 


Box mybox1 = new Box();

 

Box mybox2 = new Box();

 

double vol;

 

    get volume of first box vol = mybox1.volume();

 

System.out.println("Volume is " + vol);

 

     get volume of second box

 

vol = mybox2.volume(); System.out.println("Volume is " + vol);

 

}

 

}

 

When this program is run, it generates the following results:

 

Constructing Box

 

Constructing Box

 

Volume is 1000.0

Volume is 1000.0

As you can see, both mybox1 and mybox2 were initialized by the Box( ) constructor when they were created. Since the constructor gives all boxes the same dimensions, 10 by 10 by 10, both mybox1 and mybox2 will have the same volume. The println( ) statement inside Box( ) is for the sake of illustration only. Most constructors will not display anything. They will simply initialize an object.

Before moving on, let’s reexamine the new operator. As you know, when you allocate an object, you use the following general form:

 

class-var = new classname ( );

 

Now you can understand why the parentheses are needed after the class name. What is actually happening is that the constructor for the class is being called. Thus, in the line

 

Box mybox1 = new Box();

 

new Box( ) is calling the Box( ) constructor. When you do not explicitly define a constructor for a class, then Java creates a default constructor for the class. This is why the preceding line of code worked in earlier versions of Box that did not define a constructor. The default constructor automatically initializes all instance variables to their default values, which are zero, null, and false, for numeric types, reference types, and boolean, respectively. The default constructor is often sufficient for simple classes, but it usually won’t do for more sophisticated ones. Once you define your own constructor, the default constructor is no longer used.

 

Parameterized Constructors

 

While the Box( ) constructor in the preceding example does initialize a Box object, it is not very useful—all boxes have the same dimensions. What is needed is a way to construct Box objects of various dimensions. The easy solution is to add parameters to the constructor. As you can probably guess, this makes it much more useful. For example, the following version of Box defines a parameterized constructor that sets the dimensions of a box as specified by those parameters. Pay special attention to how Box objects are created.

 

/*     Here, Box uses a parameterized constructor to initialize the dimensions of a box.

 

*/

 

class Box { double width; double height; double depth;

     This is the constructor for Box. Box(double w, double h, double d) {

 

width = w; height = h; depth = d;

 

}

 

     compute and return volume double volume() {

 

return width * height * depth;

 

}

 

}

class BoxDemo7 {

 

public static void main(String args[]) {

 

// declare, allocate, and initialize Box objects 

Box mybox1 = new Box(10, 20, 15);

 

Box mybox2 = new Box(3, 6, 9);

 

double vol;

 

     get volume of first box vol = mybox1.volume();

 

System.out.println("Volume is " + vol);

 

     get volume of second box

 

vol = mybox2.volume(); System.out.println("Volume is " + vol);

 

}

 

}

 

The output from this program is shown here:

 

Volume is 3000.0

 

Volume is 162.0

 

As you can see, each object is initialized as specified in the parameters to its constructor. For example, in the following line,

 

Box mybox1 = new Box(10, 20, 15);

 

the values 10, 20, and 15 are passed to the Box( ) constructor when new creates the object. Thus, mybox1’s copy of width, height, and depth will contain the values 10, 20, and 15, respectively.


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.