Skip to main content

Methods

Method Arguments in Java

Method arguments are essential in programming because they provide a way to pass data to a method, allowing it to perform tasks with varying inputs.

Let's try to understand with the help of an example where we write a method to multiply two numbers.

class Main {
    
    // Method to multiply two numbers and print the result
    void multiplyNumbers() {
        int number1 = 8;
        int number2 = 6;
        int result = number1 * number2;
        System.out.println("The product of " + number1 + " and " + number2 + " is " + result);
    }
 
    public static void main(String[] args) {
 
        // Create an object of Multiplier
        Main obj = new Main();
    
        // Call the method to multiply and print the result
        obj.multiplyNumbers();
    }
}

In this code, the multiplyNumbers method multiplies two predefined numbers (number1 = 8 and number2 = 6) and prints the result. The steps to create an object and invoke the method are similar to the previous example. When you run this program, you'll see the output:

The product of 8 and 6 is 48

The multiplyNumbers() method we created earlier had limited flexibility as it was specifically calculating the multiplication of two fixed numbers, 6 and 8. Now, we'll explore how to make our method more dynamic.

In programming, we can design methods to accept input (known as arguments or parameters) and provide output (known as the return value). This allows us to create more versatile and adaptable methods.

Let's first delve into the concept of arguments. Later, we'll explore the return value aspect.

Here's a little modification to the multiplyNumbers method we previously created.

class Main {

    // Method definition with an argument
    void multiplyNumbers(int num1, int num2) {
        int result = num1 * num2;
        System.out.println("The product of " + num1 + " and " + num2 + " is " + result);
    }

    public static void main(String[] args) {

        // Create an object of Main
        Main multiplierObj = new Main();

        // Call the method with different sets of numbers
        multiplierObj.multiplyNumbers(6, 8);
        multiplierObj.multiplyNumbers(3, 5);
        multiplierObj.multiplyNumbers(10, 7);
    }
}

In the above code, we modified the multiplyNumbers() method to accept two arguments (num1 and num2). These arguments represent the numbers we want to multiply.

Note: We have added arguments inside parentheses, ().

In the main method, we created an object named multiplierObj of the Main class. We then called the multiplyNumbers() method with different sets of numbers using the object.

Depending on the numbers passed to the method, the output varies. For example:

  • For multiplierObj.multiplyNumbers(6, 8), the output is the product of 6 and 8.
  • For multiplierObj.multiplyNumbers(3, 5), the output is the product of 3 and 5.
  • For multiplierObj.multiplyNumbers(10, 7), the output is the product of 10 and 7.

When we use the multiplyNumbers method, the program goes into that method to do its job, just like before.

But this time, we're giving the method a special piece of information as an argument. Think of it like passing a note with the num1 and num2 (Eg. 6 and 8) written on it to the method. Inside the method, this note is received and the number 6 and 8 are stored in a variable num1 and num2.

Now, by changing the input numbers, we can use the same method to calculate the product for different scenarios.

Method Return Type

Until now, we've been using "void" as the return type for our methods, indicating that the methods don't return any specific value.

However, we can introduce a "return" statement inside a method.

The "return" statement:

  • Signals the end of a method's execution.
  • Sends control back to the point in the program where the method was called.

Note: When a return statement is executed, the method ends immediately, so it should be the last statement of a method.

Let's illustrate this with an example. Consider our code for finding the product of two numbers.  

let's add a return statement inside the method definition.

class Main {

    // Method definition with a return statement
    int multiplyNumbers(int num1, int num2) {
        // Calculation of product
        int result = num1 * num2;

        // Return the calculated product
        return result;
    }

    public static void main(String[] args) {

        // Create an object of NumberMultiplier
        Main multiplierObj = new Main();

        // Call the method with different sets of numbers and get the result
        int result1 = multiplierObj.multiplyNumbers(6, 8);
        int result2 = multiplierObj.multiplyNumbers(3, 5);
        int result3 = multiplierObj.multiplyNumbers(10, 7);

        // Print the results
        System.out.println("Result 1: " + result1);
        System.out.println("Result 2: " + result2);
        System.out.println("Result 3: " + result3);
    }
}

Output

Result 1: 48
Result 2: 15
Result 3: 70

Explanation

  1. Method Definition with Return Statement:
    • We modified the multiplyNumbers() method to calculate the product of two numbers and return the result using the return statement.
  2. Method Call and Result Storage:
    • In the main method, we call the multiplyNumbers() method with different sets of numbers, and each time we store the result in a variable (result1, result2, result3).
  3. Printing the Results:
    • Finally, we print the results to see the product of each set of numbers.

First time, when we call the multiplyNumbers() method with arguments(6,8). The value is assigned to num1 and num2 respectively. Inside the method, we have computed the product of numbers and returned the result using the return statement. Since the method is returning an integer value, we have assigned the method call to the integer variable result1.

Notice that we have also changed the return type from void to int.

Now, the multiplyNumbers() method not only calculates the product but also returns it, allowing us to capture and use the result in our program.