Constructor in Java

A constructor is a special method inside a class which is invoked to create an object of that class. We can also write custom constructors to initialize an object. Not just initialization, you can do lots of things inside a constructor like calling another method, opening a file, updating a database etc.


Syntax of Java Constructor

The constructor syntax is close to the method syntax but unlike any regular member method, a constructor method syntax is following restrictions:

  • The name of the constructor method should be same as the name of class.
  • There is no return type of constructor.
  • A constructor cannot be final, native, abstract, static, strictfp or synchronized.

Difference between a Constructor and Method

Constructor Method
Constructors are used to initialize an object. Member methods are used to implement object behaviors and provide an interface for other classes.
These is no return type for Constructors. Member method must have return type.
Constructor is called automatically. Member methods don't gets called automatically.
If we don't write a constructor then java compiler will create a default constructor. Java compiler don't generate member methods automatically.
The name of constructor must be same as the class name. Member Method's name can be any valid java identifier not necessarily class name.

Types of Constructors

Java support three types of constructor methods

  • Default Constructor
  • No Argument Constructor
  • Parameterized constructor

Default constructor

Defining a constructor method is optional in java. If we don't define any constructor for a class then java compiler will automatically create a constructor method without any argument and body. This constructor is known as default constructor.
For Example: Default constructor for "Rectangle" class will look like

Rectangle () {
}

No Argument Constructor

A constructor without any argument is known as no argument constructor. However it can have code inside constructor's body unlike default constructor. It is not automatically created by compiler, it is written by programmer within class. We create no argument constructor to initialize the fields of all objects with a constant value.

For Example:
Rectangle () {
 length = 0; 
 width = 0;
 height = 0;
}

Parameterized constructor

A constructor method having arguments is knows as parameterized constructor. A parameterized constructor can take any number of parameters. We use parameterized constructors to set some initial values to the member variables while object creation.


Copy Constructor

A constructor that accepts an object of its own type as a parameter and copies the data members is called a copy constructor. Copy constructors are used when we want to create a create a copy of an existing object.

For Example:
Rectangle (Rectangle rec) {
 this.length = rec.length; 
 this.width = rec.width;
 this.height = rec.height;
}

Constructor Overloading in Java

Like any other member method, we can overload constructor methods also. We can create multiple constructors having either different numbers of arguments or arguments of different data types. We use different constructors when we want to initialize some or all of member variables.