What is a Constructor
A constructor is a special type of method whose name is same as class name. It is used to initialize an object (not to create the object). The constructor is used to give initial values to the instance variables defined by the class. The name of the constructor should be the same as that of the class name and never contain any return type including void.
Constructor in Java
Every Java class has a constructor and it is automatically called at the time of object creation. The main purpose of constructor is to initialize the object after creating an object. Constructors are invoked using the new operator and it cannot have an explicit return type.
Java runtime identifies normal methods and constructor is by its return type.
public Car() { System.out.println("Car Constructor"); } public Car Car() { System.out.println("Car Method"); return new Car(); } |
In this Car class example first one is a constructor because there is no return type. The Second example is a method because it returns a new Car object.
We can call constructors in two ways.
- Test t = new Test();
- new Test(); (No need to create reference)
Constructors execute automatically when we create an object. When no constructors are created, a default constructor is created automatically.
Syntax:
Class ClassName { ClassName() { } } |
All classes have constructors, whether you define one or not, Java automatically provides a default constructor that initializes all member variables. The default constructor is a zero-argument constructor with an empty body.
Types of Constructors in Java
- Default Constructor (Create by compiler)
- No-Args Constructor (Create by the programmer)
- Parameterized Constructor (Create by the programmer)
Default Constructor in Java
If we don’t provide a constructor implementation in the class, then java provides a default constructor with no arguments. The body of the constructor is empty.
Let’s look at an example program:
public class Car { public static void main(String[] args) { Car car = new Car(); } } |
In this example, we haven’t defined an explicit constructor hence java will create a default constructor.
The default constructor is used to give initial values to the instance variables defined by the class.
No-Args Constructor
Constructor with no arguments is called no-args Constructor. Signature is same as that of the Default Constructor. The body can have any code. It is like overriding the default constructor and it can be used for initialization of logging, database connection etc. Let’s look at an example:
public class Car { public Car() { System.out.println("No-Args Constructor"); } public static void main(String[] args) { Car car = new Car (); } } |
When we call new Car(), it will call no-args constructor.
Parameterized Constructor
Constructor with arguments is known as the Parameterized Constructor.
When we create a parameterized constructor compiler will not call the default constructor.
Let’s look at an example:
public class Car { private String color; public Car(String c) { System.out.println("Parameterized Constructor"); this.color = c; } public String getColor() { return color; } public static void main(String[] args) { Car car = new Car("Red"); System.out.println(car.getColor()); } } |
Constructors in Inheritance
When a derived class is extended from the base class, the constructor of the base class is executed first, followed by the constructor of the derived class. When there are multiple constructors in the parent class, the constructor without any parameters is called from the child class.
If we want to call the constructor with parameters of the parent class, we can use super keyword.
Super(a, b); -> Calls the constructor from the parent class which takes 2 variables.
Constructor during Constructor overloading
When we have more than one constructor in a java class it is called constructor overloading. The compiler differentiates constructors based on how many arguments are present in the constructor and the order in which the they are passed.
Constructor overloading example:
public class Car { private String model; private String color; public Car() { //no-args constructor this.color = "white"; //default color } public Car(String m) { //one parameter constructor this.model = m; } public Data(String m, String c) { //two parameter constructor this.model = m; this.color = c; } public String getModel() { return model; } public String getColor() { return color; } @Override public String toString() { return "Model="+model+", Color="+color; } public static void main(String[] args) { Car c = new Car(); System.out.println(c); c = new Car("Red"); System.out.println(c); c = new Car("Verna", “Black”); System.out.println(c); } } |
Access Modifiers in Constructor
A constructor cannot be declared as final, static, synchronized or abstract. The only modifiers applicable for constructor are public, protected, default and private.
If we make a constructor private then other class won’t be able to create the instance of the class. For singleton design pattern implementation, we have to use private constructor.
// private constructor private Car() { } |
Copy Constructor
The copy constructor is used to create an exact copy of another object. If we make any change in the copy, it will not affect in the original one. It can also be achieved by using Cloning or Deep copy methods.
Let’s look at an example:
public class Car { private String model; private String color //copy constructor public Car(Car car) { //copying each filed this.model = car.model; this.color = car.color; } } } |