Calling a Method

Now that we can write separate methods, we need to be able to call them (tell the compiler that we want to execute the code in the method). Suppose our program looks like this, which includes some of the methods written above and some new ones:

public class Methods 
{
    public static void main(String[] args) 
    {
        //The code we write in this section goes here
    }

    //This method prints every value in the nums array
    public static void printArray(int[] nums) 
    {
        for (int i = 0; i < nums.length; i++) 
        {
            System.out.println(nums[i]);
        }
    }

    //This method returns the number of times letter appears in str
    public static int countLetter(String str, char letter) {
        int count = 0;
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == letter) count++;
        }

        return count;
    }

    // This method returns the area of the rectangle with
    // length length and width width
    public static double getArea(double length, double width) 
    {
        return length*width;
    }

    //This method asks the user for 10 numbers,
    //and prints the sum of those numbers
    public static void sum10() 
    {
    int sum = 0;
        Scanner s = new Scanner(System.in);
        for (int i = 0; i < n; i++) {
            System.out.print("Enter an integer: ");
            int val = s.nextInt();
            sum += val;
        }
        System.out.printf("The sum is: %d%n", sum);
    }
}

Calling a void method

Here is the syntax for calling a method with a void return type:

ClassName.methodName(params);

Here, ClassName is the name of the class that we’re using, and methodName is the name of the method that we want to run. Finally, params is a comma-separated list of values that you want to pass to the method. Each value can be a variable name, a constant value, or an expression (like a math operation). You do not list the type of the parameters when you call the method, but their types should match the parameter types in the method declaration.

Suppose we are inside the main method in the Methods class above. Let’s say we want to call the printArray method. First, we create an array of integers:

//creates space for the array and initializes the values
int[] vals = {1, 2, 3, 4};

Now, we are ready to call the printArray method. Here’s how:

Methods.printArray(vals);

Notice that we just list “vals” as the name of the parameter. This variable has the same type as the parameter in the printArray method declaration – an int[].

When we call the printArray method, we pass the vals array. The printArray parameter is named nums, so nums gets initialized to be this vals array. When we print the elements in nums inside the method, it is really printing the elements from vals.

Suppose we are still inside the main method in the Methods class, and that we now want to call the sum10 method. Here’s how:

Methods.sum10();

This method does not take any parameters, but we still end the method call with (). Now, our entire main method looks like this:

public static void main(String[] args) 
{
    int[] vals = {1, 2, 3, 4};
    Methods.printArray(vals);
    Methods.sum10();
}

When our program is executed, it starts with the very first statement in the main method. This statement creates our vals array. Next, we execute the call to the printArray method. This method prints every element in our array. When the printArray method finishes, the program returns back to the spot in the code where that method was called (which is our main method). So the very next thing that we do is execute the call to the sum10 method. This asks the user for 10 different numbers, and prints their sum. When this method finishes, it returns back to the main method. That’s the end of the main method, so our program ends.

Calling a non-void method

Calling a method that returns a value is a bit trickier. For now, the syntax for calling one of these methods is:

type name = ClassName.methodName(params);

Here, we are declaring a variable called name and setting it equal to the result of the method call. This will store the value returned by the method in the name variable. The type of the name variable must match the return type of the method.

For example, suppose we are in the main method in the Methods class, and we want to compute the area of a 3x4 rectangle. Here’s how:

int result = Methods.area(3.0, 4.0);

When this method is called, 3.0 is passed into the length parameter, and 4.0 is passed into the width parameter. The method returns the calculation 3.0 * 4.0, which gets stored in our result variable.

Next, suppose we want to call the countLetter method from the main method in the Methods class. Suppose that we first want to ask the user to input a string and character to use, and then we want to give those values to the countLetter method. Here’s how:

Scanner s = new Scanner(System.in);
System.out.print("Enter a word: ")
String word = s.nextLine();
System.out.print("Enter a letter to search for: ");
char c = (s.nextLine()).charAt(0);
int numTimes = Methods.countLetter(word, c);

When we call countLetter, we pass the value for the word inputted by the user (which gets stored in the str parameter variable) and the value for the character inputted by the user (which gets stored in the letter parameter variable). The method returns the number of times that letter appears in the string, and this result gets stored in the numTimes variable.