Methods

The terms method, procedure, and function all, broadly speaking, mean the same thing: a sub-program that does a specific task within a larger program. Different programming languages use the terms in slightly different ways, but in Java, any such sub-program is referred to as a method.

Methods help break a program into smaller and modular chunks. As a method performs a discrete task, it can be tested when it is written, to make sure it works as expected. As a program grows larger and larger, methods help make the program more organized and manageable. Furthermore, methods can help to avoid repetition and make code more reusable, because different data values can be passed into the method when it is executed.

In Java, a method might calculate and return a value, or a method may do something without returning anything.

A method that returns a value

Here is a Java method that is part of a class called PizzaDelivery.

public double area(int radius)

{

    return 3.14 * radius * radius;

}

The first line is called the method header. This header tells us that the method can be used from anywhere in the program (public), it returns a value that is a double, it has the name area, and it requires an int value to be supplied for its radius. The information to be supplied is called the parameter, or sometimes argument, of the method. If we need multiple parameters, we separate them with commas.

The body of the method, which is between the braces, calculates the area of a circular pizza and the keyword return is used to return the resulting value.

The method area can be executed from elsewhere in the program. In Java, such a method must always be executed using a software object that understands it, in this case an object of type PizzaDelivery. If we have created such an object, let us say myPizzaDelivery, and we want to calculate and return the area of a pizza with a radius of 6 inches, we can call the method area on the object like this:

myPizzaDelivery.area(6);

Resulting in the value 113.04 being returned.

The area of a pizza with a radius of 4 inches can be found by executing:

myPizzaDelivery.area(4);

Resulting in the value 50.24 being returned.

A method that doesn't return a value

Not all methods return a value, for example here is a method, which might also be written in the PizzaDelivery class, that prints something to the screen:

public void printGoodbyeMessage()

{

    System.out.println("Thank you for ordering pizza");

}

Notice how this method has the Java keyword void in its header, indicating that it does not return a value. It also has no parameters (there is nothing in the parentheses in the method header), which means that it needs no additional data in order to perform its task.

As there is no value to be returned in this case, the Java keyword return is not used in the method body.

As before, this method must be executed on an object of type PizzaDelivery, e.g.:

myPizzaDelivery.printGoodbyeMessage();

Java compared with Python

Java always uses the term method to refer to a subprogram, but in Python the terms function and method are both used. A Python method is, like a Java method, executed on an object and so can access the data within the object and change it. A Python function does not belong to a particular class of object.

For comparison, here is the Java area method we showed above written as a Python function, which is defined using the keyword def:

def area(radius):

  return 3.14 * radius * radius

Test yourself

  1. A method in Java must always return something. True or false?
  2. A method in Java may have a parameter. True or false?
  3. Write a Java method called printWelcomeMessage that prints to the screen "Welcome to PizzaDelivery".
  4. Write a Java method called sliceArea that calculates and returns the area of a square slice of pizza. The user of the method must provide the width and length of the slice as int parameters and the area will be returned as an int.