Professional Documents
Culture Documents
Java Constructor
Chapter-02
Mohammed Husain Bohara
1
7/13/2019
Example
class Test
{
// logic here
}
2
7/13/2019
3
7/13/2019
Example
class Test /*
{ Test()
{
void m1() // empty implementation
{ }
S.O.P.(“M1 method”); */
}
P.S.V.M(String[] args){ Zero argument constructor
Test t = new Test();
} Type of default constructor
}
4
7/13/2019
Types of constructor
1. Default Constructor
(i) Always Zero argument constructor NOTE:
Default Constructor generated
by compiler and executed by
2. User define Constructor JVM
(i) Zero argument constructor
(ii) Parameterized constructor
P.S.V.M(String[] args){
Test t = new Test();
Test t1 = new Test(10);
t.m1(); t1.m1();
}
}
5
7/13/2019
P.S.V.M(String[] args){
Test t = new Test();
Test t1 = new Test(10);
}
}
1. Declare instance variable as length & width 1. Initialize object here (main
2. Create Rectangle constructor to initialize method)
instance variable 2. Set the length and width
3. Create setLength & setWidth method 3. Print area and perimeter
4. Create area and perimeter method and
return values to main method
6
7/13/2019
7
7/13/2019
Case-01
class Emp{
int eid;
String ename;
float esal; Output:
void display(){ S.O.P.(“Emp id is : ”, +eid);
0
S.O.P.(“Emp name is : ”, +ename);
Null
S.O.P.(“Emp salary is : ”, +esal); } 0.0
P.S.V.M(String[] args){
Emp e = new Emp();
e.display();
}}
Case-02
class Emp{
int eid;
String ename;
float esal;
Output:
Emp() { eid=123; ename = “XYZ”, esal= 1000;}
123
void display(){ S.O.P.(“Emp id is : ”, +eid); XYZ
S.O.P.(“Emp name is : ”, +ename); 1000
S.O.P.(“Emp salary is : ”, +esal); }
P.S.V.M(String[] args){
Emp e = new Emp();
e.display();
}}
Constructor uses to initialize some values to instance variable during object creation
8
7/13/2019
Parameterize constructor
9
7/13/2019
Constructor calling
Example-1
class Test
{ Test()
{ this(10);
S.O.P.(“0-argument constructor”); }
Test(int a)
{ this(10,20);
S.O.P.(“1-argument constructor”); }
Test(int a, int b)
{ S.O.P.(“2-argument constructor”); }
P.S.V.M(String[] args){
Test t = new Test();
}}
10
7/13/2019
P.S.V.M(String[] args){
Test t = new Test();
}}
11
7/13/2019
12
7/13/2019
13
7/13/2019
NO
Reason- No, because compiler and JVM considers it as a method.
14
7/13/2019
15
7/13/2019
by new Keyword
16
7/13/2019
Copy Constructor
There are many ways to copy the values of one object into another in
java. They are:
• By constructor
• By assigning the values of one object into another
• By clone() method of Object class
17
7/13/2019
WHAT ?
For example, let us assume a class namely Car and it has a constructor
called copy constructor which expects only one argument of type Car.
WHY ?
• Duplicate object in the sense the object will have the same
characteristics of the original object from which duplicate object is
created.
• But we have to ensure that both original and duplicate objects refer
to different memory locations.
18
7/13/2019
Example
class CopyConstructor{
int id;
String name; public static void main(String args[]){
CopyConstructor(int i,String n){
id = i; CopyConstructor s1 = new
name = n; CopyConstructor(111,"Karan");
}
CopyConstructor s2 = new CopyConstructor(s1);
CopyConstructor(CopyConstructor s){
id = s.id;
name =s.name;
s1.display();
}
void display() s2.display();
{
System.out.println(id+" "+name); }
} }
19
7/13/2019
Example
class CopyConstructor{
int id; public static void main(String args[]){
String name; CopyConstructor s1 = new
CopyConstructor(int i,String n){ CopyConstructor(111,"Karan");
id = i; CopyConstructor s2 = new CopyConstructor();
name = n;
s2.id=s1.id;
}
s2.name=s1.name;
CopyConstructor(){}
s1.display();
void display()
s2.display();
{ }
System.out.println(id+" "+name); }
}
20
7/13/2019
finalize() method
class Thing {
public int number_of_things = 0;
public String what;
public class TestDestructor {
21
7/13/2019
Any Question?
A. It print 0
22
7/13/2019
Ans- D
Ans- D
23
7/13/2019
Ans- B
Ans- D
24
7/13/2019
Reference
https://deepeshdarshan.wordpress.com/2013/12/05/copy-
constructors-in-java/
25