Java Constructor Example

Hey guys!! Welcome to flower brackets blog. Today we are going to learn java constructor example.

java constructor example

 

What is a constructor?

Basically constructor in java is a method, rather a special method to initialize an object.

Java Constructor Example

Here’s the example of constructor in java,

public class Employee
{
   private String name;
   private int age;

   public Employee(String name, int age)
   {
      this.name = name;
      this.age = age;
   }   
}

In the example above you can see “Employee” class, it has two attributes name and age.

It has one “Employee” constructor which accepts name and age. Using this constructor we can create “Employee” object.

Also Read – Continue Java Example

So the definition of constructor is, a class contains constructors that are invoked to create objects from the class.

Constructor name should match with the class name. Constructor declarations looks like method declaration except the constructor use the name of the class and constructor has no return type.

Let’s see an example now,

public class Employee
{
   private String name;
   private int age;

   public Employee(String name, int age)
   {
      this.name = name;
      this.age = age;
   }
   
   public String getName()
   {
      return name;
   }

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

   public int getAge()
   {
      return age;
   }

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

   public static void main(String args[])
   {
      Employee samEmployee = new Employee("Sam", 23);
      System.out.println("Employee Name : " + samEmployee.getName());
      System.out.println("Employee Age : " + samEmployee.getAge());
      System.out.println("----------------------------------");
      Employee benEmployee = new Employee("Ben", 28);
      System.out.println("Employee Name : " + benEmployee.getName());
      System.out.println("Employee Age : " + benEmployee.getAge());
   }
}

Output:

Employee Name : Sam
Employee Age : 23

--------------------------------------

Employee Name : Ben
Employee Age : 28


Java constructor example : Types of constructor in java

There are three types of constructor in java namely,

  • Default constructor
  • No-argument constructor
  • Parameterised constructor

Let’s see three constructor types with an example

Default Constructor

Default constructor is a constructor where you don’t define constructor in a class.

Let’s see an example with some explanation,

public class Employee
{
   String name;
   int age;
}

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

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

Now here I have defined the class “EmployeeDemo” and it has “main” method. After running the above example, this is the output we get,

Output:

Employee Name : null
Employee Age : 0

As you can see the output, in the “main” method i’m invoking no argument constructor to create new “emp” object.

Then using that object I’m accessing name and age fields and displaying the output.

If you see the “Employee” class we did not define any no argument constructor.

But what compiler will do is, it will automatically provide a no argument default constructor if the class does not have any other constructor.

The default constructor will call no argument constructor of the superclass.

If the superclass does not have no argument default constructor, then the compiler will throw compile time error.

And if the class does not define any superclass, implicit superclass is object. And object has no argument default constructor.


No-argument constructor

is constructor with no arguments.

No- Argument Example:

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

Parameterised constructor

Parameterised constructor means it can accept few arguments while creating the object.

Argument can be of any type, that is, integer, character, array or an object. Here constructor can take any number of arguments.

So let’s see an example,

public class EmployeeDemo
{
   public static void main(String args[])
   {
      Employee sam = new Employee("Sam", 23, "Software");
      System.out.println("Employee Name : " + sam.getName());
      System.out.println("Employee Age : " + sam.getAge());
      System.out.println("Employee Department : " + sam.getDepartment());
   }
}

public class Employee
{
   private String name;
   private int age;
   private String department;

   // Parameterised constructor
   public Employee(String name, int age, String department)
   {
      this.name = name;
      this.age = age;
      this.department = department;
   }
 
   public String getName()
   {
      return name;
   }

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

   public int getAge()
   {
      return age;
   }

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

   public String getDepartment()
   {
      return department;
   }

   public void setName(String department)
   {
      this.department = department;
   }
}


private constructor

Here we are going to learn private constructor. Let’s understand private constructor with an example,

public class Employee
{
   private String name;
   private int age;

   // Private constructor
   private Employee()
   {
      System.out.println("We are inside Employee() constructor");
   }
 
   public String getName()
   {
      return name;
   }

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

   public int getAge()
   {
      return age;
   }

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

Here I have defined “Employee” class and it has employee constructor with private access modifier.

public class EmployeeDemo
{
   public static void main(String args[])
   {
      Employee emp = new Employee();
      System.out.println("Employee Name : " + emp.getName());
      System.out.println("Employee Age : " + emp.getAge());
   }
}

Here I have defined the class “EmployeeDemo”. It has main method. First we will see the output,

Output:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
      The constructor Employee() is not visible
      at EmployeeDemo.main(EmployeeDemo.java:7)

As you can see the output we got compile time error. Because the employee constructor has private access modifier.

Hence we cannot create object using private constructor. So we got error saying the constructor employee is not visible.


Overloaded constructors

Let’s see an example first,

public class Employee
{
   private String name;
   private int age;
   
   public Employee()
   {
      System.out.println("We are inside Employee() constructor");
   }

   public Employee(String name)
   {
      System.out.println("We are inside Employee(String name) constructor");
      this.name = name;
   }

   public Employee(String name, int age)
   {
      System.out.println("We are inside Employee(String name, int age) constructor");
      this.name = name;
      this.age = age;
   }

   public Employee(int age)
   {
      System.out.println("We are inside Employee(int age) constructor");
      this.age = age; 
   }

   public String getName()
   {
      return name;
   }
   
   public void setName(String name)
   {
      this.name = name;
   }

   public int getAge()
   {
      return age;
   }
   
   public void setAge(int age)
   {
      this.age = age;
   }
}

Here you can see overloaded constructors, name of the constructors is same but each constructors has different parameters.

In java constructors are differentiated on basis of the number of arguments in the list and types.

Also Read – ArrayList Class Java

Above I have defined “Employee” class with two attributes name and age. Just below “Employee” class I have defined four constructors.

And these constructors are overloaded constructors because parameters are different.

public class EmployeeDemo
{
   public static void main(String args[])
   {
      Employee emp1 = new Employee();      
      System.out.println("Employee Name : " + emp1.getName());   
      System.out.println("Employee Age : " + emp1.getAge());

      System.out.println("\n");

      Employee emp2 = new Employee("Sam"); 
      System.out.println("Employee Name : " + emp2.getName()); 
      System.out.println("Employee Age : " + emp2.getAge());

      System.out.println("\n");

      Employee emp3 = new Employee("Ben", 23); 
      System.out.println("Employee Name : " + emp3.getName()); 
      System.out.println("Employee Age : " + emp3.getAge());

      System.out.println("\n");

      Employee emp4 = new Employee(28); 
      System.out.println("Employee Name : " + emp4.getName()); 
      System.out.println("Employee Age : " + emp4.getAge());
   }
}

Let’s see the output first,

Output:

We are inside Employee() constructor

Employee Name : null
Employee Age : 0

We are inside Employee(String name) constructor

Employee Name : Sam
Employee Age : 0

We are inside Employee(String name, int age) constructor

Employee Name : Ben
Employee Age : 23

We are inside Employee(int age) constructor

Employee Name : null
Employee Age : 23


Please note

public class Employee
{
   private String name;
   private int age;
   
   public Employee(String name)
   {
      System.out.println("We are inside Employee(String name) constructor");
      this.name = name;
   }

   public Employee(String empName)
   {
      System.out.println("We are inside Employee(String empName, int age) constructor");
      this.name = empName;
      this.age = age;
   }
}

Here you can see two employee constructors that have the same number and type of arguments for the same class.

This is not possible in java. Compiler will throw compile time error.


conclusion

That’s it guys. This is all about constructors in java. I hope you have understood the concept.

You can subscribe to my blog flower brackets if you haven’t already.

Do share this article if you like.

You May Also Like