Constructor in java

Let’s learn constructor in java.

Constructor in java

Constructor is a block of code used to initialize objects.

constructor in java

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:

public class ConstructorDemo
{
   int a;
   ConstructorDemo()
   {
      a = 26;
   }
   public static void main(String[] args)
   {
      ConstructorDemo obj = new ConstructorDemo();
      System.out.println(obj.a);
   }
}


Output:

26


Types of constructor

  • Default constructor
  • No-arg constructor
  • Parameterised constructor

Default constructor in java

Default constructor is a constructor created by compiler; if user does not create a constructor in a class.

If user defines a constructor in a class then java compiler will not create default constructor.

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();
   }
}


Now let’s see an example on default constructor that prints or displays default values.

Here I have not created constructor. Compiler provides a default constructor.

class DefaultConstructorDemo 
{
   int empID;  
   String empName;  
   void print()
   {
      System.out.println(empID + " " + empName);
   }
   public static void main(String[] args) 
   {
      // here we are creating objects  
      DefaultConstructorDemo obj1 = new DefaultConstructorDemo();  
      DefaultConstructorDemo obj2 = new DefaultConstructorDemo();  
      // print values of object  
      obj1.print();  
      obj2.print();
   }
}


Output:

0 null
0 null

Output 0 and null values are provided by default constructor.


No-argument (no-arg constructor) constructor

No-argument (no-arg constructor) constructor is a constructor with no arguments.

If a class has constructor with arguments then compiler does not create default constructor.

class NoArgumentDemo
{
   NoArgumentDemo()
   {
      System.out.println("no argument constructor.");
   }
   public static void main(String[] args)
   {
      NoArgumentDemo obj = new NoArgumentDemo();
   }
}


Output:

no argument constructor.


Parameterized constructor

Parameterized constructor is a constructor which has fixed number of parameters.

Also read – method in java

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.

Also read – this keyword in java

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


Copy constructor

Java copy constructor is special type of constructor used to create an object using another object of same class.

Copy constructor takes argument of same class and provides copy of specified object.

NOTE: copy constructor are not inherited by subclass.

class Student
{
   private int empID;
   private String empName;
   // java copy constructor
   Student(Student student)
   {
      this.empID = student.empID;
      this.empName = student.empName;
   }
}