Java

How to Add Two Numbers Using Parameterized Constructor in Java

Object-oriented programming offers java constructors that are used to initialize/allocate the memory to the newly created object. In java, the constructor of a class gets executed when we create an object of the class using the “new” keyword. In java, a constructor can be a “Default Constructor” or a “Parameterized Constructor”. The parameterized constructors allow us to initialize each instance of a class with different value(s). In this write-up we will learn how to perform addition on two numbers using a Java parameterized constructor.

This post will present a step-by-step guide to assist you with the below-listed learning outcomes:

Before heading towards the main topic(i.e., how to add two numbers using the parameterized constructor), first, we need to understand what precisely a parameterized constructor is and how it works in Java. So, let’s start!

What is a Constructor in Java

A constructor is a method/function having precisely the similar name as the class name, doesn’t have any return type, and will be called/invoked automatically whenever we create the object of that class. In java, a constructor can be parameterized or non parameterized.

What is Java Parameterized Constructor

It can accept some parameters/arguments and we write it explicitly. The main goal of a parameterized constructor is to assign the values of the user’s choice to the data members of the class.

How Parameterized Constructors Work in Java

In this section, first, we will learn how to create and call a parameterized constructor and afterward, we will understand how a parameterized constructor works in java.

Constructor Creation

Let’s consider the following syntax to understand the concept of how to create parameterized constructor:

public class ParameterizedConstructor {
    ParameterizedConstructor(int number1, int number2, int number3)
    {
        //code
    }
}

The above snippet shows that the class name and constructor name are the same, and it accepts three parameters.

Constructor Calling

In java, the values will be passed to the parameterized constructor at the time of the constructor call, as shown in the below snippet:

ParameterizedConstructor myObj = new ParameterizedConstructor (val1, val2, val3);

Example

In this example, we will create a parameterized constructor that will accept two values as arguments, and we will print both the values:

public class ParameterizedConstructor {
    ParameterizedConstructor(int number1, int number2) {
        System.out.println("First Value: " + number1);
        System.out.println("Second Value: " + number2);
    }
    public static void main(String[] args) {
        ParameterizedConstructor myObj = new ParameterizedConstructor(14, 52);
    }
}

We passed two values, “14” and “52”, to the parameterized constructor, the constructor received and stored them in “number1” and “number2”, respectively. Finally, we printed both the values using System.out.println():

This is how a parameterized constructor works in Java.

How to Add Two Numbers Using Java Parameterized Constructor

As of now we have learned what is a parameterized constructor and how it works in Java. Now we will hit our main target i.e. we will calculate the sum of two numbers using a parameterized constructor:

public class ParameterizedConstructor {
    ParameterizedConstructor(int number1, int number2) {
        int result;
        result = number1 + number2;
        System.out.println("Sum: " + result);
    }

    public static void main(String[] args) {
        ParameterizedConstructor myObj = new ParameterizedConstructor(40, 56);
    }
}

We passed two values, “40” and “56”, to the parameterized constructor; the constructor received them in “number1” and “number2” and stored their sum in “result”. Finally, we printed the sum of both numbers using System.out.println():

The output showed that the parameterized constructor successfully calculated the sum of two numbers.

Conclusion

In java, a constructor that can accept some parameters/arguments is called a parameterized constructor. The values will be passed to the parameterized constructor at object creation. The constructor will accept the values and perform some functionality as defined within the body of the parameterized constructor (in our case, constructor will add two values).

This write-up provided a step-by-step guide on adding two numbers using a parameterized constructor in java.

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.