Java Constructors Explained

A constructor is a special method in Java. It is called when an instance of object is created and memory is allocated for the object. It is called constructor because it constructs the values at the time of object creation. It is not necessary to write a constructor for a class. It is because java compiler creates a default constructor if your class doesn’t have any.

When is a Constructor called ?

Each time an object is created using new() keyword at least one constructor (it could be default constructor) is invoked to assign initial values to the data members of the same class.

Rules & Characteristics java constructor
  • Constructor name must be same as its class name
  • An interface cannot have the constructor.
  • Constructors cannot be private.
  • A constructor cannot be abstract, static, final, native, strictfp, or synchronized
  • A constructor can be overloaded.
  • Constructors cannot return a value.
  • Constructors do not have a return type; not even void.
  • An abstract class can have the constructor.
  • Constructors are automatically called when an object is created.
Types of Constructors
  1. Default Constructor
  2. Parameterized Constructor

constructors in java

Default Constructor

A constructor that has no parameter is known as default constructor. If we don’t define a constructor in a class, then compiler creates default constructor(with no arguments) for the class. You would not find it in your source code(the java file) as it would be inserted into the code during compilation and exists in .class file. This process is shown in the diagram below:

default constructor in java

You can also create your default constructor if you want to initialize some values in the object data members.

Following is a program example –

Constructor Called
Number value is: 5

In the example above, you can see that the default constructor is called when the object is instantiated using the new keyword. Thus the constructor is used to construct the values of objects.

Parameterized Constructor

A constructor that has parameters is known as parameterized constructor. If we want to initialize fields of the class with your own values, then use parameterized constructor.

Following is a Program example for the same –

Parameterized Constructor Called
Number value is: 4

In the above example you can see the parameterized constructor is called when we pass a value in the opening and closing parathesis. Since our parameterized constructor in this example takes in 1 int value as argument, we have to pass 1 integer value during the object intialization in order to invoke the parameterized constructor.

What if you implement only parameterized constructor in class?

If you try to run this program, it will give you a compilation error. Here you have created a parameterized constructor, but you have not specified a default no args constructor. This the reason why you get that error. When while creating the object in the main function (since in the main function when you are creating the object you are not passing any args to invoke the parameterized constructor).

When you implement any constructor (in above example I have implemented parameterized constructor with int parameter), then you don’t receive the default constructor by compiler into your code. If we remove the parameterized constructor from the above code then the program would run fine, because then compiler would insert the default constructor into your code.

Constructor Overloading

Like methods, we can overload constructors for creating objects in different ways. Compiler differentiates constructors on the basis of numbers of parameters, types of the parameters and order of the parameters.

Program Example –

Default Constructor Called
Num1: 1
Num2: 1.5
Parameterized Constructor 1 Called
Num1: 5
Num2: 0.0
Parameterized Constructor 2 Called
Num1: 5
Num2: 5.5
Simple Snippets

In the above example you can see we have 3 constructors – 1 default and 2 parameterized, thus we have achieved constructor overloading.

Watch it on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *