Professional Documents
Culture Documents
Constructor is a member method with a name same as that of the class name used to initialize
the instance variables of the objects.
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.
Syntax-
<class name>()
:
:
Invoking a constructor
No return type
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
A constructor is used to initialize the state of an A method is used to expose the behavior of an
object. object.
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
Default Constructor
class Cons
int a;
boolean b;
String c;
char m;
double d;
a=0;
b=false;
c= “ “;
m=’ ‘;
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) {
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
int i , j;
parametrizeCons(int a, int b)
i=a;
j=b;
}
void display()
System.out.println("j = "+j);
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
{ int a,b;
double c, d,e;
consOver()
a=0;
b=0;
}
consOver(int x, int y)
a=x;
b=y;
c=x;
d=y;
e=z;
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;