Java

Constructor in Java | Explained

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:

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:

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:

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;

public class ConstructorExample {
 int num1, num2;
 ConstructorExample() {
 System.out.println("The Default Constructor Invoked ");
  num1 = 100;
  num2 = 75;
 }
 void sum(){
 int sum;
 sum = num1+num2;
 System.out.println("Sum of the values is " + sum);
 }
 public static void main(String[] args) {
 ConstructorExample obj = new ConstructorExample();
 obj.sum();
 }
}

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

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:

How to invoke a Parameterized Constructor

The below snippet shows how to invoke a parameterized constructor:

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;
public class ConstructorExample {
  int a, b;
  ConstructorExample(int number1, int number2) {
  System.out.println("The Default Constructor Invoked ");
  a = number1;
  b = number2;
 }
  void sum(){
  int sum;
  sum = a+b;
  System.out.println("Sum of the values is " + sum);
 }
  public static void main(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:

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

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.