Constructor in Java
What is meant by Constructor in Java?
- Constructor is a method like structure.
- Whose name is the same as that of the class name.
- Does not return any value – NOT EVEN ‘void’.
- Constructor helps to assign the values for the data members of the class.
- Constructor is used along the ‘new’ operator to create an OBJECT
- Constructor is an optional.
- If you don’t define any explicit constructor, the JVM will provide a ‘default’ constructor to the class.
- In addition, default Constructor – It is a constructor without any parameters and assigns a minimum/default to all the data members of the class.

Constructors in Java programming: Different Types
There are two types of constructors in Java, they include
1) Parameterized Constructor
2) Default Constructor
What is a Parameterized Constructor in Java?
If we pass values to a constructor, it is known as a parameterized constructor.
What is a Default Constructor in Java?
If case of no arguments, it is a default constructor. The JVM assigns the minimum values and runs the program for the default constructor in Java.
For instance, for ‘int,’ it gives a zero value.
For ‘void,’ it doesn’t provide anything. Generates a blank space.
For ‘float’, it gives a 0.0 value.
What is the Constructor Syntax in Java?
The syntax of the constructor is as follows,
class name() {}
The example from the program for the constructor syntax is as follows.
Employee(int empId, String empName, float empBasic) { this.empId = empId; this.empName = empName; this.empBasic = empBasic;}
Explain the Concept Constructor in Java Class
While defining the constructor, we define it inside the class in Java.
Method and Constructor
Though a constructor looks like a method or function, there are differences between them in terms of the following,
1) Importantly, the name of the class is inclusive in the constructor. For example, if the class name is Employee, the constructor is Employee() {}
2) However, constructor doesn’t return a value, whereas a method or function returns a value.
3) Finally, While the constructor helps assign the values, the method in Java provides business logic.
public void calculateTotalPay(int days) { System.out.println("Gross Pay Amount : " + (empBasic * days));}
Example of Method in Java with Business Logic
What is the Importance of Constructor?
Moreover, constructor plays an important role during Object Creation:
During object creation, we are calling the constructor.
An Example Program for Constructor
public class Employee{ int empId; // Data Members String empName; float empBasic; Employee(int empId, String empName, float empBasic) { // Constructor this.empId = empId; this.empName = empName; this.empBasic = empBasic; } public void calculateTotalPay(int days) { System.out.println("Gross Pay Amount : " + (empBasic * days)); // Member Functions } public void displayEmployee() { System.out.println("Employee Id: " + empId); System.out.println("Employee Name : " + empName); System.out.println("Employee Basic : " + empBasic); } public static void main(String[] args) // Main Function { Employee e = new Employee(1001, "Raju", 1234.56f); // During object creation, we are calling the constructor. e.displayEmployee(); e.calculateTotalPay(24); }}
Output
Employee Id: 1001Employee Name : RajuEmployee Basic : 1234.56Gross Pay Amount : 29629.44
For expert guidance, click on the link.