- Constructor is a special method which is used to
initialize the state of an object. - Constructor is special method because it has following
properties:
– Automatically called
– Constructor name is same as class name
– No return type
- Programmer can’t call constructor explicitly. It is called
automatically at the time of creation of object. - Constructor implicitly returns this (reference of current object).
Types of Constructor
Default Constructor
Default constructor does not take any argument and it is
used to initialize default values of an object.
used to initialize default values of an object.
class A { A() //default constructor { System.out.println("default constructor"); } public static void main(String...s) { new A(); } }
Parameterized Constructor
Constructor having one or more arguments is called parameterized constructor. Example of parameterized constructor is given below.
class A { A(int x) { System.out.println(x); } public static void main(String...s) { new A(10); } }
Copy Constructor
Copy constructor is a type of parameterized constructor. It
is used to copy existing object values into another object at its creation
time.
is used to copy existing object values into another object at its creation
time.
class temp { int x,y; temp(int x,int y) //parameterized constructor { this.x=x; this.y=y; } temp(temp t) //copy or parameterized constructor { this.x=t.x; this.y=t.y; } void show() { System.out.println(x); System.out.println(y); } public static void main(String...s) { temp t1=new temp(10,20); t1.show(); temp t2=new temp(t1); t2.show(); } }
- In java each class has constructor. If we do not write any
constructor in our class then compiler will add a default constructor
implicitly. This can be proved by decompiling the .class file as shown below.
- Constructor can be overloaded same as like method overloading.
- Constructor can be private but can’t be final (constant).
Observe below program:
class temp { temp() { System.out.println("constructor"); } void temp() { System.out.println("function"); } public static void main(String...s) { new temp(); new temp().temp(); } }
In above program we have used temp() two times. Have you thought
how compiler differentiate between temp() and void temp()? It is done by their positions,
the first temp() is treated as constructor while the next temp() is treated as method.
Constructor Chaining
Calling one constructor form another constructor is known as
constructor chaining. Below program shows the concept of constructor chaining.
constructor chaining. Below program shows the concept of constructor chaining.
class temp { temp() { this(10); System.out.println("default"); } temp(int x) { this(10,20); System.out.println(x); } temp(int x,int y) { System.out.println(x+" "+y); } public static void main(String...s) { new temp(); } }
Rules:
- this can’t be
used into normal method to call constructor. - this must be
first line of constructor. - We can use constructor chaining only when object is created
using new keyword. - Order of calling doesn’t matter.