Object-oriented programming provides a concept of constructors that allows us to initialize the newly created objects. A constructor is a special type of method/function having the same name as the class name and a constructor doesn’t have a return type. This write-up presents a comprehensive guide for the java constructors and in this regard, it elaborates the following concepts:

  • Constructor in Java
  • Constructor Types in Java
  • Basic Syntax of Default and Parameterized Constructor in Java
  • How to invoke Default and Parameterized Constructor in Java
  • How to Use Default and Parameterized Constructor in Java

So, let’s get started!

Constructor in Java

A java constructor has a primary motive of initializing the objects, i.e. the constructor is used to specify an initial value to the instances and it will be invoked automatically whenever we create an object of the class. Within Java constructor, we can specify a block of code the same way as we write code in a normal method.

Constructor Types

There are two types of constructors in Java:

  • default constructor
  • parameterized constructor

Default Constructor

Every Java class has a default constructor and it doesn’t take any value as an argument.

Syntax

The basic syntax of the default constructor is shown in the below-given snippet:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/word-image-4.png" data-lazy- height="187" src="data:image/svg xml,” width=”432″>

The above figure clarifies that the class name and constructor name are the same and the constructor doesn’t have a return type.

How to invoke a Default Constructor In Java

The default constructor will be invoked automatically at the time of object creation. The below-given snippet shows how the object is created:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/word-image-6.png" data-lazy- height="96" src="data:image/svg xml,” width=”565″>

In the above snippet, an object is created, and as a result, the default constructor will be called automatically.

How to Use Default Constructor in Java

The below-given example will let you understand how a default constructor works.

Example

Let’s consider the below code snippet, we create a default constructor for the “ConstructorExample” class, and inside the constructor, we initialize the value for the class attributes “num1” and “num2”:

package constructorexample;

publicclassConstructorExample {

int num1, num2;


ConstructorExample() {


 System.out.println(“The Default Constructor Invoked “);


  num1 = 100;


  num2 = 75;


 }


voidsum(){

int sum;


 sum = num1 num2;


 System.out.println(“Sum of the values is “ sum);


 }


publicstaticvoidmain(String[] args) {


 ConstructorExample obj = new ConstructorExample();


 obj.sum();


 }

}

The complete code and its respective output is shown in the below-given screenshot:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/word-image-11.png" data-lazy- height="578" src="data:image/svg xml,” width=”727″>

Whenever an object of the class will be created, the above code will print a message “The Default Constructor Invoked ” and values of the class attributes num1, num2 will be initialized with 100 and 75 respectively. Lastly, sum of the two numbers initialized in the constructor is also shown in the method of the “ConstructorExample” class.

Parameterized Constructor

It can take a specific number of values as arguments, these values are referred as the parameters, and the constructor having a certain number of parameters can be referred as a parameterized constructor.

Syntax

The below-given figure shows the basic syntax of the parameterized constructor:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/word-image-13.png" data-lazy- height="102" src="data:image/svg xml,” width=”611″>

How to invoke a Parameterized Constructor

The below snippet shows how to invoke a parameterized constructor:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/word-image-14.png6225493ba0477.jpg" data-lazy- height="226" src="data:image/svg xml,” width=”628″>

In the parameterized constructor, we have to pass the values for the parameters while creating the object.

How to Use Parameterized Constructor in Java

The below-given example will provide a detailed understanding of how a parameterized constructor works:

Example

The below piece of code creates a parameterized constructor that takes two parameters int number1, int number2.

package constructorexample;


publicclassConstructorExample {

int a, b;


  ConstructorExample(int number1, int number2) {


  System.out.println(“The Default Constructor Invoked “);


  a = number1;


  b = number2;


 }


voidsum(){


  int sum;


  sum = a b;


  System.out.println(“Sum of the values is “ sum);


 }


publicstaticvoidmain(String[] args) {


  ConstructorExample obj = new ConstructorExample(120, 210);


  obj.sum();


 }

}

Within the constructor we specify a = number1 and b = number2 and when we invoked the constructor we passed values i.e. 120, 210. The sum() function will add both values and displays the sum.

The below snippet will show the complete code along with output:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/word-image-16.png" data-lazy- height="582" src="data:image/svg xml,” width=”651″>

The output verifies that the sum function provides the sum of the values provided in parameters to the constructor.

Conclusion

Every class has a constructor and it will be invoked whenever an object of the class is created. It may or may not take the parameters, the one which didn’t take any parameter is referred as the default constructor and the other one which takes parameter is referred as the parameterized constructor. This write-up presents a comprehensive overview of what is Java Constructor and what are its types, how to use the default and parameterized constructor.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/FA9AFCF66E2D44C3A65378AC87A87EB4-150×150.jpg6225493bd41fe.jpg" height="112" src="data:image/svg xml,” width=”112″>

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.