Professional Documents
Culture Documents
Constructors
3.0 Introduction
Whenever you define a class, a special user defined class type, gets created for
use in java codes. Software objects can then be created out of that class type and used in
java programs similarly as a conventional data type.
When an object is created from a defined class, use of the new operator is made
where class-name() calls the default constructor for creating an object of class-name
type. A constructor determines how an object will be initialized when created.
A java programmer may include explicitly one or more constructor (s) while defining a
class. If no explicit constructor is specified within a program, Java automatically supplies a
default constructor. The initialization of the created object is just carried out either by the
default constructor [when no constructor method is defined explicitly in a class] or by one of
the programmer’s supplied Constructors.
A constructor can be regarded as a special member function whose name will be the
same as that of the class being initialized. However, no return type – even void -- is used
with that special member function. This is because a constructor always returns an object
of the respective class type.
This chapter will deal with different aspects of default constructor, constructors with
and without any parameters, etc. Constructor Overloading (i.e. use of multiple
constructors) is allowed in a class definition – and discussion about them will also be made
giving appropriate examples.
When a programmer does not specify a constructor explicitly in a class, Java supplies
a default constructor for that class. The default constructor always initializes all instant
variables with either zero or null value(s).
When a constructor remains defined within a class, the default one is no longer used.
For simple classes, default constructors may serve the purpose, but for more efficient and
effective initialization, a programmer will have to define his/her own constructor(s) as per
application specific requirements.
Suppose you define a class DefaultCube as shown in example-3.1
/**
* a method to
* display cube volume
*/
public void volumeMethod()
{
// codes used in the method
int volume = length* width* height;
System.out.println("Volume of the default Cube is :" + volume);
}
}
This shows that why initialization with desired values of instant variables of a class is
necessary.
We all know that a cube has equal length, width and height. Therefore, just a single
value of “size” will be sufficient to determine the volume of a cube. So we can define a class
cube with a constructor having a single parameter only. Example-3.2 shows a single
parametric constructor for the class Cube.
/**
• Constructor defined
• With a single parameter
*/
public Cube( double x)
{
width = x;
height =width;
length = width;
}
/**
* a method - computing volume
*/
public void volume()
{
double vol = width*height*depth;
System.out.println ("Volume of the Specified Cube =" + vol);
}
}
/**
• Constructor for objects of class SolidBox
• With three parameters
*/
public SolidBox( double w, double h, double l)
{
width =w;
height =h;
length =l;
}
/**
* a method - to compute volume
*/
public void volumeSBox()
{
double vol = width*height* length;
System.out.println(" Volume of the Solid Box = " + vol);
}
Picture 3.2
Like methods, constructors can also be overloaded. That means, a class can have
more than one constructor (using the same name as that of the class) but each one should
have different parametric declarations.
Such constructor overloading can help object initialization with different values
under different situations. Let us look at an example of overloading constructors.
Suppose you want to define a class Solid3D, which can be initialized either as a
Cube-object, or as a solid object of any length, width and height. For that we have to define
two separate constructors for the class Solid3D.
/**
* Constructor for objects of class Solid3D
*/
return length*width*height ;
}
}
The class Solid3D will be able to create two different types of objects – one type as
cubes and another type as a solid of any dimensions. For cubes, only one parameter is
to be passed, whereas for any size solid – three parameters are to be passed for
initialization.
A controlling DemoSolid class with a main () method can now utilize this
Solid3D class, defined with two constructors. DemoSolid will be able to create different
category of objects having any desired dimensions and will compute and display their
respective volumes as shown in example-3.5.
vol2 = solidx.volMethod();
System.out.println(" Volume of the solid is :" + vol2);
}
} // The results appear on the Terminal Window shown below (Picture 3.3)
Picture 3.3 Outputs of example-3.5
Example 3.5 shows how multiple constructors can help a java programmer to take
care of different application requirements. This example also tells about the association of
more than one classes and the importance of the class with the main () method.
The new operator creates objects by taking help of the constructor(s), but by which
function those objects are to be destroyed and the occupied memory space can be released for
run-time reallocation is another important question. A destructor function can take care of
that release operation. A destructor function (not used in java but used by C++) can be
specified as
A destructor cannot accept any argument. Being a function, it obeys the usual
access rules. Sometimes an object may need to perform some action(s) before it gets
destroyed. To handle such situations, java provides a mechanism called finalization. A
finalizer can be added within a class by using a finalize () method.
The protected keyword prevents access to finalize function by the external codes. The java run
time is only allowed to call that method whenever object destruction with finalization is needed.
Java’s system takes care of unused objects to free memory space automatically as and
when needed. This technique of automatic destruction of objects is known as garbage
collection. Thus a java programmer is made free from the burden of destroying unused objects
explicitly. Of course, C++ programmers will have to make use of destructor(s) explicitly.
3.5 Conclusions
The destructor concept has been explained briefly. In Java, destruction process is
taken care of by the system’s garbage collector. The importance of finalizes () function
is also made clear.