Learn constructor in java

Hey guys!! how are you doing!!? Welcome to flower brackets blog. In this post we are going to learn constructor in java.

Learn Constructor In Java

Constructor in java is used to initialize instance variables of an object. It is rather a special method to initialize an object. Constructor is similar to method.

Also read – this keyword in java

NOTE :

  • constructor will 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 after instantiation. Here’s an example,

Syntax:

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

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

What is the purpose of a constructor in a class?

  • 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.
  • constructor is the first method which will be executed.

Types of constructors

  • Default constructor
  • No-argument constructor
  • Parameterised constructor

Default constructor

Constructor without any parameters is known as default constructor. Java compiler automatically creates new default constructor if a class does not have or there is no default constructor.

Also read – string constructors in java

Now, what is the purpose of default constructor in java? Let’s see an example with some explanation,

public class Employee
{
   String name;
   int age;
   Employee()
   {
      System.out.println("Employee constructor called");
   }
}

In the example above I have defined class “Employee”. It has two attributes name and age with Employee() constructor

public class EmployeeDemo
{
   public static void main(String args[])
   {
      Employee emp = new Employee(); // invoking default constructor
      System.out.println("Employee Name : " + emp.name);
      System.out.println("Employee Age : " + emp.age);
   }
}

Output:

Employee constructor called

Employee name : null

Employee age : 0

As you can see the output first default constructor “Employee()” is called. Then default constructor initializes member variables to default values, that is, null and 0.

Also read – area of rectangle using constructor overloading in java

The purpose of default constructor is to initialize instance variables with fixed values depending on data type.


No-argument constructor

No-argument constructor is a constructor with no arguments.

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


Parameterised constructor

Parameterised constructor is a constructor with parameters or arguments. Argument can be of any type, that is, integer, character, array or an object. Here constructor can take any number of arguments and with our own values.

class ConstructorDemo
{ 
   // data members 
   String empName; 
   int empID;

   // parameterised constructor 
   ConstructorDemo(String empName, int empID) 
   { 
      this.empName = empName; 
      this.empID = empID; 
   }
}
public class Example
{
   public static void main(String args[])
   {
      // invoking parameterized constructor 
      ConstructorDemo cd = new ConstructorDemo("Sachin", 226); 
      System.out.println("Employee name : " + cd.empName + " and Employee ID : " + cd.empID);
   }
}

Output:

Employee name : Sachin and Employee ID : 226


Constructor overloading

Constructor overloading is an approach of having more than one constructors in a class with different arguments performing different tasks.

Also read – area of rectangle using parameterised constructor in java

Constructors can be overloaded like methods. Let’s see constructor overloading in java,

public class EmployeeData 
{
   private int empID;
   private String empName;

   // default constructor
   EmployeeData()
   {
      empID = 10014;
      empName = "John"; 
   }
   // constructor overloading
   EmployeeData(int ID, String name)
   {
      empID = ID;
      empName = name; 
   }

   public int getEmpID() 
   {
      return empID;
   }
   public void setEmpID(int empID) 
   {
      this.empID = empID;
   }
   public String getEmpName() 
   {
      return empName;
   }
   public void setEmpName(String empName) 
   {
      this.empName = empName;
   } 
}

public class ConstructorOverloadingDemo 
{
   public static void main(String[] args) 
   {
      // calling default constructor
      EmployeeData emp = new EmployeeData();
      System.out.println("Employee name : " + emp.getEmpName());
      System.out.println("Employee id : " + emp.getEmpID());

      // calling overloaded constructor 
      EmployeeData emp2 = new EmployeeData(10015, "Charles");
      System.out.println("Employee name : " + emp2.getEmpName()); 
      System.out.println("Employee id : " + emp2.getEmpID()); 
   }
}

Output:

Employee name : John
Employee id : 10014
Employee name : Charles
Employee id : 10015

Related Posts