Let’s learn constructor in java.
Constructor in java
Constructor is a block of code used to initialize objects.

NOTE:
- constructor does not have any return type.
- constructor name is same as class name.
- constructor is called when an object of a classs is created.
- access modifiers applicable for constructors are public, private, protected and default.
- constructor gets called when we write “new()” keyword.
Syntax:
class DemoClass { // constructor name is same as class name DemoClass() { .... } } // calls DemoClass() constructor DemoClass obj = new DemoClass();
Constructor example:
// create ConstructorDemo class public class ConstructorDemo { int a; // class attribute // create constructor for ConstructorDemo class ConstructorDemo() { a = 26; // initial value for class attribute 'a' } public static void main(String[] args) { // creating object for ConstructorDemo class // here we call constructor ConstructorDemo() ConstructorDemo obj = new ConstructorDemo(); System.out.println(obj.a); } }
Output:
26
Types of constructor
- No-arg constructor
- Parameterised constructor
No-argument (no-arg constructor)
No-argument (no-arg constructor) is a constructor with no arguments.
If a class do not have constructor then compiler creates default constructor with no arguments.
Let’s see an example on no-arg constructor.
class NoArgExample { int number; String name; NoArgExample() { System.out.println("Constructor called"); } } public class NoArgConstructorDemo { public static void main(String[] args) { // this will invoke default constructor. NoArgExample obj = new NoArgExample(); // default constructor provides default // values to object like 0, null System.out.println(obj.name); System.out.println(obj.number); } }
Output:
Constructor called
null
0
NOTE: In a java program if there is no constructor in a class then compiler automatically creates default constructor.
Here’s how it looks in .class file,
// Employee.java class Employee { public static void main(String args[]) { // invoking default constructor Employee emp = new Employee(); } } // Employee.class class Employee { Employee() { } public static void main(String args[]) { Employee emp = new Employee(); } }
Parameterized constructor
Parameterized constructor is a constructor which has fixed number of parameters.
Parameterized constructor main purpose is to provide different values to objects. Let’s see an example on parameterized constructor.
class Employee { int empID; String empName; // parameterized constructor Employee(int ID, String name) { empID = ID; empName = name; } void print() { System.out.println(empID + " " + empName); } public static void main(String args[]) { Employee obj1 = new Employee(232, "dhoni"); Employee obj2 = new Employee(233, "virat"); obj1.print(); obj2.print(); } }
Output:
232 dhoni
233 virat
Constructor overloading in java
Constructor overloading is an approach of having more than one constructors in a class with different arguments performing different tasks.
Constructors can be overloaded like methods. Let’s see a java program to overload constructors.
class Employee { int empID; String empName; int empAge; // two argument constructor Employee(int id, String name) { empID = id; empName = name; } // three argument constructor Employee(int id, String name, int age) { empID = id; empName = name; empAge = age; } void print() { System.out.println(empID + " " + empName + " " + empAge); } public static void main(String args[]) { Employee obj1 = new Employee(232, "dhoni"); Employee obj2 = new Employee(233, "virat", 26); obj1.print(); obj2.print(); } }
Output:
232 dhoni 0
233 virat 26