Constructor in java

In this post we are going to learn constructor in java.

constructor in java

Constructor is a method used to initialize object. Constructor is similar to method.

NOTE:

  • constructor never return value.
  • constructor name is same as class name.
  • static, abstract, final keywords can’t be used for constructors.

Constructor is called when we write “new()” keyword or when object is created.

Syntax:

class DemoClass
{
   // constructor name is same as class name
   DemoClass()
   {
      ....
   }
}

DemoClass obj = new DemoClass();  // calls DemoClass() constructor

Constructor java example:

class ConstructorDemo
{
   int salary;
   ConstructorDemo()
   {
      this.salary = 60000;
   }

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


Output:
60000


What is the purpose of a constructor?

  • constructor is used to allocate memory to the object.
  • constructor is used to initialize values.
  • even if you don’t define a constructor, java will create a default constructor when you create a class. But initial values can’t be set.
  • constructor is the first method which will be executed.

Types of constructors

  • Default constructor
  • No-argument constructor
  • Parameterised constructor

Default constructor

Java compiler adds default constructor with no arguments with empty definition if a class does not have or there is no constructor.

Let’s see an example,

// Employee.java
public class Employee
{
   public static void main(String args[]) 
   { 
      Employee emp = new Employee(); // invoking default constructor 
   }
}

// Employee.class
public class Employee
{
   Employee() 
   {
       
   }
   public static void main(String args[])
   {
      Employee emp = new Employee();
   }
}



No-argument constructor

No-argument constructor is a constructor with no arguments. If a class has constructor with no-argument constructor then compiler does not create default constructor.

class Car
{
   public Car()
   {
      System.out.println("Car constructor called");
   }

   public static void main(String args[])
   {
      new Car();
   }
}



Output:

Car constructor called


Parameterized constructor

Parameterized constructor is a constructor with parameters. Parameters can be of any type, that is, integer, character, array or an object.

Here in the below java program we have default and parameterized constructor.

If there is no parameter, default constructor is invoked. Else parameterized constructor gets invoked.

class Student
{
   private int ID;
   // default constructor
   public Student()
   {
      this.ID = 2625;
   }

   // parameterized constructor 
   Student(int stuID) 
   { 
      this.ID = stuID; 
   }

   public int printID()
   {
      return ID;
   }

   public static void main(String[] args) 
   {
      Student obj1 = new Student();
      Student obj2 = new Student(2626);
      System.out.println("ID : " + obj1.printID());
      System.out.println("ID : " + obj2.printID());
   }
}



Output:

ID : 2625
ID : 2626


Constructor overloading in java with examples

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,

class EmployeeDetails 
{
   private int empID;
   private String empName;
   private int empAge;

   EmployeeDetails()
   {
      // default constructor
      empID = 51615;
      empName = "new employee";
      empAge = 28;
   }

   EmployeeDetails(int ID, String strName, int age)
   {
      // parameterized constructor
      empID = ID;
      empName = strName;
      empAge = age;
   }

   // getter and setter methods
   public int getEmpID() 
   {
      return empID;
   }

   public void setEmpID(int ID) 
   {
      this.empID = ID;
   }

   public String getEmpName() 
   {
      return empName;
   }

   public void setEmpName(String name) 
   {
      this.empName = name;
   }

   public int getEmpAge() 
   {
      return empAge;
   }

   public void setEmpAge(int age) 
   {
      this.empAge = age;
   }

   public static void main(String args[])
   {
      // call the default constructor
      EmployeeDetails obj1 = new EmployeeDetails();
      System.out.println("Employee name : " + obj1.getEmpName());
      System.out.println("Employee age : " + obj1.getEmpAge());
      System.out.println("Employee ID : " + obj1.getEmpID());
      // calling parameterized constructor
      EmployeeDetails obj2 = new EmployeeDetails(15566, "Virat", 29);
      System.out.println("Employee name : "+ obj2.getEmpName());
      System.out.println("Employee age : " + obj2.getEmpAge());
      System.out.println("Employee ID : " + obj2.getEmpID()); 
   }
}

Output:

Employee name : new employee
Employee age : 28
Employee ID : 51615
Employee name : Virat
Employee age : 29
Employee ID : 15566


Constructor chaining in java with examples

Constructor chaining means when a constructor calls another constructor of same class.

NOTE: one constructor calls another constructor using “this” keyword.

class Demo 
{
   public String stuName;
   public int stuID;
   public String details;

   // default constructor
   public Demo()
   {
      // calling the constructor with String parameter
      this("Virat");
   }

   public Demo(String name)
   {
      // calling constructor with (String, int) parameter
      this(name, 1648);
   }

   public Demo(String name, int ID)
   {
      // calling constructor with (String, int, String) parameter
      this(name, ID, "Delhi");
   }

   public Demo(String name, int ID, String detail)
   {
      this.stuName = name;
      this.stuID = ID;
      this.details = detail;
   }

   void printDetails() 
   {
      System.out.println("Student name : " + stuName);
      System.out.println("Student ID : " + stuID);
      System.out.println("Student details : " + details);
   }
}


// main method
public class ConstructorChainingDemo 
{
   public static void main(String[] args) 
   {
      Demo obj = new Demo();
      obj.printDetails();
   }
}



Output:

Student name : Virat
Student ID : 1648
Student details : Delhi


Java copy constructor

Java copy constructor is special type of constructor to copy values of one object to another object.

Copy constructor is used as an alternative for cloning.

class CopyConstructorDemo 
{
   String name; 
   CopyConstructorDemo(String str)
   { 
      name = str;
   }

   // copy constructor
   CopyConstructorDemo(CopyConstructorDemo copy)
   { 
      name = copy.name; 
   }

   void print()
   {
      System.out.println("demo : " + name);
   }

   public static void main(String args[])
   { 
      CopyConstructorDemo obj1 = new CopyConstructorDemo("HelloWorld"); 
      // passing object as an argument to constructor
      // hence invoking copy constructor
      CopyConstructorDemo obj2 = new CopyConstructorDemo(obj1);
      obj1.print(); 
      obj2.print(); 
   }
}



Output:

demo : HelloWorld
demo : HelloWorld