Constructors in Java are used to create the instance of a class. These are similar to the methods in Java except for a difference: the constructor name is the same as that of the class name and it does not return a value. The main purpose of using a constructor is to build an object and assign the values to the created object. The constructor mainly comprises three types that are used in coding. The Constructors in Java are the Default constructor, parameterized constructor, and no argument constructor.
In this write-up, we will demonstrate in detail the implementation of the constructor along with its types.
What are Constructors in Java?
The constructor used in Java are much similar to the methods of Java and these constructors are invoked when an object is created for a class. It makes the use of the same name as that of the declared class. If the constructor is not declared in the code, Java creates a default constructor but the values for the objects can not be set. The other two types of constructors are no argument and parameterized constructors that can be declared according to the need.
Let us have a look at the general syntax of the constructor in Java
class Example {
Example() {
// constructor body
}
}
In the syntax above the Example() is the constructor; the constructor and the class are of the same name that is Example.
Example 1: Constructor Implementation Using Integer and Strings in Java
The code below depicts the constructor that is declared as Main() in Java.
class Main {
private Integer i;
private String name;
//Declaring the Java constructor
Main() {
//Declaring Integer and string in Java
System.out.println("JAVA CONSTRUCTOR");
i=21;
name = "Ali";
}
//The Main class in Java
public static void main(String[] args) {
//The object of Main class.
Main obj = new Main();
//The output statement of Java.
System.out.println("Student ID is " + obj.i);
System.out.println("Student name is " + obj.name);
}
}
In the above Java code:
- A class is declared as Main with an initialized string and integer.
- After that, the constructor Main() is declared.
- The constructor contains the integer and the string values as i and name respectively.
- In the next step, an object is created In the main class of Java.
- The println() method prints the ID and the Name.
Output
The output below depicts that the student ID and student name are printed using the Main constructor of Java.

Example 2: Default Constructor in Java
In Java, when an object is created, by default, the default constructor is invoked to execute the code.
class Employees{
Employees(){
System.out.println("The default Constructor");
System.out.println("The employee name is Ali");
System.out.println("The employee designation is Banker");
}
//The Main method of Java
public static void main(String args[]){
//The default constructor in Java
Employees obj=new Employees();
}
}
In the above code:
- A class is declared as “Employees”.
- The println() method prints the statement declared in the Employees class.
- The main method of Java comprises the object for the Employees class which is actually a default constructor of Java.
Output
The output below shows that the default constructor is automatically invoked and the output is printed as the employee name and the designation.

Example 3: Parameterized Constructor of Java
The constructor that accepts one or more than one parameter is called a Parameterized constructor. It is also referred to as the constructor with the parameters. Here is an example that illustrates how the parameterized constructor works in Java:
class courses{
int id;
String coursename;
//Parameterized constructors that contain two parameters
courses(int i,String n){
id = i;
coursename = n;
}
//The display method to display the ID and coursename
void display(){
System.out.println(id+" "+coursename);}
//The main class of Java
public static void main(String args[]){
courses obj1 = new courses(1," Python for Beginners");
courses obj2 = new courses(22, "Data Structures for Intermediate Level");
//calling method to display the values of the object
obj1.display();
obj2.display();
}
}
In the above code:
- A class declared as a “courses” contains the id and the coursename.
- The parameterized constructor has two arguments declared as integer i and String n.
- The display() method displays the ID and the coursename using the println() method.
- The Main class of Java consists of two objects obj1 and obj2 that prints the result accordingly.
Output
The below output depicts that the integer value is printed along the string value that is the ID and the course name are printed.

Example 3: No Argument Constructor in Java
When there are no parameters passed in the constructor it is referred to as “No Argument Constructor”. The code below shows that no arguments are passed and the constructor is declared as private.
class constructor {
String x;
// constructor with no parameter
private constructor() {
System.out.println("No-Argument Constructor");
x="Example of Private No-Argument Constructor";
}
//The Main class that contains no parameters
public static void main(String[] args) {
constructor obj = new constructor();
System.out.println(obj.x);
}
}
In the above code:
- The string x is declared in the constructor class.
- The private constructor comprises the print statement and the integer x.
- The object for the main class is declared as obj which is printed using the println() method of Java.
Output
The output below shows the implementation of a private no-argument constructor to print the string passed in x.

Example 5: No-Argument Public Constructor of Java
The code below shows that the constructor is declared as public and no arguments are passed to it.
class constructor {
String x;
// constructor with no parameter
public constructor() {
System.out.println("No-Argument Constructor");
x="Example of Public No-Argument Constructor";
}
//The Main class that contains no parameters
public static void main(String[] args) {
constructor obj = new constructor();
System.out.println(obj.x);
}
}
The above code is the same as the previous one except for the difference that the constructor is declared as public.
Output
The output below shows that the No-Argument constructor is declared as public.

Conclusion
The constructors in Java initialize the objects that are created. The constructor in Java is referred to as the block of code that is similar to the method except for the difference that it does not return a value and uses the same name as that of the class. In this article, we have implemented three distinct types of constructors along with the code examples in Java.