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.

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 or when object is created.

Syntax:

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

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

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.

Also read – string constructors in java

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

Also read – area of rectangle using constructor overloading in java


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


Parameterised constructor

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

Here constructor can take any number of parameters or with our own values.

class Employee
{ 
   // data members 
   String name; 
   int ID;

   // parameterised constructor 
   Employee(String empName, int empID) 
   { 
      name = empName; 
      ID = empID; 
   }

   public static void main(String args[])
   {
      // invoking parameterized constructor 
      Employee emp = new Employee("Sachin", 226); 
      System.out.println("Employee name : " + emp.name + " and Employee ID : " + emp.ID);
   }
}

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 java program on constructor overloading in java,

public class EmployeeData 
{
int id;
String name;
int age;

// two argument constructor
EmployeeData(int empID, String empName)
{
id = empID;
name = empName;
}

// three argument constructor
EmployeeData(int empID, String empName, int empAge)
{
id = empID;
name = empName;
age = empAge;
}

void print()
{
System.out.println(id + " " + name + " " + age);
}

public static void main(String args[])
{
EmployeeData emp1 = new EmployeeData(101, "Sachin");
EmployeeData emp2 = new EmployeeData(202, "Virat", 25);
emp1.print();
emp2.print();
}
}

Output:

101 Sachin 0
202 Virat 25

Related Posts