Declaring a Method

This section will discuss the format for declaring a method. All methods must have a return type (the type of result the method is computing and giving back), a name, and a number of parameters (also called arguments – pieces of data that the method needs to perform its calculations).

Syntax

Methods are declared inside the brackets for the class, but outside the brackets for the main method. For example:

public class Example 
{
    //Can declare method(s) here

    public static void main(String[] args) 
    {

    }

    //Can declare method(s) here

    //Can declare as many methods as you want
}

Here is the syntax for declaring a method:

public static returnType name(params) 
{
    //code for method goes here
}

For now, don’t worry about what the public static part means – just put that at the beginning of all your methods.

Next, the returnType is the type of data you want this method to give back. For example, if we were writing a method that computed the area of a square with integer sides, then we would want to return an int – the data type of the area. If you do not want your method to give back a result, then the return type should be void.

The name of method should be descriptive of what the method is doing. For example, a method that computes the area of a square should be called something like getArea or getAreaSquare. The rules for naming methods are the same as the rules for naming variables: method names can be made up of letters, numbers, and underscores, but they cannot start with a number. It is convention in Java that method names use camel case, where the first word in the method is all lower-case but each subsequent word in the name is capitalized.

Finally, the params are a comma-separated list of values that the method needs to do its calculations. Each parameter is listed with a data type and a name. If the method does not need any values to do its calculations, then the params section is left off the method declaration (but the () at the end of the method is still there).

Examples

Suppose we want to declare a method that computes and returns the area of a rectangle. First, let’s think about what information this method needs to do its computation – these will be the parameters. To compute the area of a rectangle, we need its width and its height. Next, we need to determine the data type of these values. We know that the width and height will be numbers, but they can be either ints or doubles – depending on what kind of values we want to allow. We’ll be more general and make them doubles.

Next, we need to consider what kind of value we’re computing. We want to return the area of a rectangle. Each side of the rectangle is a double, and so the area (and return type) should also be a double.

Here is the declaration for the method that will compute the area of a rectangle:

// This method returns the area of the rectangle with
// length length and width width
public static double getArea(double length, double width) 
{
    //we are just declaring the method now, not implementing it
}

As a second example, suppose we want to declare a method that prints all of the values in an array of integers. This method needs to take in the integer array as a parameter, but it doesn’t need to return anything (so it will have a void return type). Here is the declaration:

//This method prints every value in the nums array
public static void printArray(int[] nums) 
{

}

Finally, suppose we want to declare a method that returns the number of times a particular character appears in a string. This method needs to take the string we’re examining and the letter we’re looking for as parameters. It needs to return a count of how often that letter is in the string – an int. Here is the declaration:

//This method returns the number of times letter appears in str
public static int countLetter(String str, char letter) 
{

}