CONSTRUCTOR
Constructor is a member method with a name same as that of the class name used to initialize
the instance variables of the objects.
NEED OF USING A CONSTRUCTOR
When we create various objects of a class, data members are automatically allocated under
each object. If you are allowed to initialize data members at the time of creating a class then
the data members corresponding to all the objects will possess the same initial values. But in
practice we would like to have separate initial values for different objects. In case we use any
member method to initialize the data members, we may need to call it separately every time
after creating an object.
This is why constructor is created.
Syntax-
class <class name>
<data member 1>
<data member 2>
<data member n>
<class name>()
<data member 1 = value>
<data member 2 = value>
:
:
<data member n = value>
Invoking a constructor
At the time of creating an object
Item ob = new item()-------- constructor
The constructor is always public.
No return type
Only used to initialize the data member not for calculation.
Some facts –
i) Constructor as a function should have the same name as the class name
ii) as a constructor is used to initialize the object’s variable, it does not need to use return type.
iii) The constructor is not called through any object but it is automatically called when an object
is declared
Java Constructor Java Method
A constructor is used to initialize the state of an A method is used to expose the behavior of an
object. object.
A method must have a return type.
A constructor must not have a return type.
A constructor name is always same as the A method does not possess a same class
class name name
It is used to initialize the data members It is used to do the work on data members.
Need of Constructor
Think of a Box. If we talk about a box class then it will have some class variables (say length,
breadth, and height). But when it comes to creating its object(i.e Box will now exist in
computer’s memory), then can a box be there with no value defined for its dimensions. The
answer is no.
So constructors are used to assign values to the class variables at the time of object creation,
either explicitly done by the programmer or by Java itself (default constructor).
TYPES OF CONSTRUCTOR
*Default constructor
i) Object supplied by compiler
ii) Object supplied by the programmer
Default Constructor
class Cons
int a;
boolean b;
String c;
char m;
double d;
Cons()// default constructor
a=0;
b=false;
c= “ “;
m=’ ‘;
d=0.0;
public static void main(String[] args) {
// A default constructor is called (constructor automatically evoked)
Cons obj = new Cons();
System.out.println("a = " + obj.a); 0
System.out.println("b = " + obj.b); false
System.out.println("c = " + obj.c);
System.out.println("m = " + obj.m);
System.out.println("d = " + obj.d); 0.0
In the above program, we have not initialized the value of both the variables, a and b. However,
when we create an object of the class, we can see in the output that the values are initialized
with some values.
It is because the Java compiler has automatically created a default constructor. The constructor
will initialize the value of variables a and b with default values 0 and false.
class Cons1
int a;
int b;
Cons1()
a=5;
b=10;
}
public static void main(String[] args) {
// A default constructor is called
Cons1 obj = new Cons1();
System.out.println("a = " + obj.a);
System.out.println("b = " + obj.b);
In the above program if we initialize the value of both the variables a and b using constructor at
the time of object creation now the values of a and b will be changed.
* Parameterized constructor – A function with the same name of a class can
be used to initialize the instance variables of the object by using values from the
parameter list .The instance variables are initialized through the parametric
values passed while creating an object .Such a constructor is known as
Parameterized constructor.
i) compiler created object
ii) object created by programmer.
//Parameterized constructor
public class parametrizeCons
int i , j;
parametrizeCons(int a, int b)
i=a;
j=b;
}
void display()
System.out.println("i = "+ i);
System.out.println("j = "+j);
public static void main()
parametrizeCons obj = new parametrizeCons(4, 5);
obj.display();
CONSTRUCTOR OVERLOADING
A process of using a number of constructors with the same name but different types of
parameter lists is known as Constructor overloading.
//Constructor overloading
public class consOver
{ int a,b;
double c, d,e;
consOver()
a=0;
b=0;
}
consOver(int x, int y)
a=x;
b=y;
consOver(double x, double y, double z)
c=x;
d=y;
e=z;
public static void main()
consOver obj = new consOver();
consOver obj1 = new consOver(4,5);
consOver obj2 = new consOver(4.2,5.5,6.0);
//displaying the constructor value through objects
System.out.println("a="+obj.a +"b="+obj.b );
System.out.println("a="+obj1.a +"b="+obj1.b );
System.out.println("c="+obj2.c +"d="+obj2.d+"e="+obj2.e );
}
constructor(){
{a=x;
b=y;
constructor(intx, int y)
a=x;
b=y;