In Java, a constructor is a block of codes similar to the method. It is called when an instance of the class is created. At the time of calling constructor, memory for the object is allocated in the memory.
It is a special type of method which is used to initialize the object.
Every time an object is created using the new() keyword, at least one constructor is called.
It calls a default constructor if there is no constructor available in the class. In such case, Java compiler provides a default constructor by default.
Rules for creating Java constructor
There are two rules defined for the constructor.
1)Constructor name must be the same as its class name
2)A Constructor must have no explicit return type
A Java constructor cannot be abstract, static, final, and synchronized
There are two types of constructor
*Default constructor
*Parameterized constructor

Default constructor: Default constructor (No-arg constructor)
A no-arg constructor doesn’t accepts any parameters, it instantiates the class variables with their respective default values (i.e. null for objects, 0.0 for float and double, false for Boolean, 0 for byte, short, int and, long).
There is no need to invoke constructors explicitly these are automatically invoked at the time of instantiation.
Rules to be remembered
While defining the constructors you should keep the following points in mind.
1)A constructor does not have return type.
2)The name of the constructor is same as the name of the class.
3)A constructor cannot be abstract, final, static and Synchronized.
4)You can use the access specifiers public, protected & private with constructors.
Code example
class NumberValue { private int num;
public void display() {
System.out.println("The number is: " + num);
}
}
public class Demo {
public static void main(String[] args) {
NumberValue obj = new NumberValue();
obj.display();
}
}
Output:
The number is: 0
Parametirised constructor
A Constructor with arguments (or you can say parameters) is known as Parameterized constructor. As we discussed in the Java Constructor tutorial that a constructor is a special type of method that initializes the newly created object. We can have any number of Parameterized Constructor in our class.
Code:
public class Name {
String S;
Name(String S)
{
this.S = S;
}
public void disp()
{
System.out.println("Her name is : "+S);
}
public static void main(String args[])
{
//Calling the parameterized constructor
Name c = new Name("Khushi");
c.disp();
}
}
Output
Her name is : Khushi
Constructor overloading in java:

Constructor overloading is a concept of having more than one constructor with different parameters list, in such a way so that each constructor performs a different task.
The order of the parameters of methods.