Methods in Java Classes: Introduction

Fundamental concepts and terminology used in the definition and invocation of methods.

Overview

Nearly all programming languages provide a facility for defining a high-level operation, or unit of processing instructions, with a name or other identifier, zero or more inputs, and zero or more outputs. After being defined, such an operation can be invoked, with input values corresponding to its defined inputs, and the output (if any) can be used—assigned to a variable, used as input to another unit of processing, etc.

We can think of the act of defining such an operation as “teaching” the computer a new capability, and the act of invoking it as triggering its execution. Defining these operations is a large part of what we do when we define classes in Java.

Conceptual example

Teacher

Do you know how to convert a Celsius temperature to Fahrenheit?

Student

No, I don’t.

Teacher

Well, let me teach you:

For a given temperature expressed in Celsius degrees—let’s call it $C$—you simply compute $\frac{9C}{5} + 32$; the result is the temperature in Fahrenheit degrees, which we can refer to as $F$. In other words, for any numeric value $C$, you simply compute $F = \frac{9C}{5} + 32$, and give back the computed value $F$ as the answer. Let’s call this the “convert Celsius to Fahrenheit” operation.1

(Note: The above is an example of a definition of an operation—in this case, one for converting from Celsius to Fahrenheit.)

For example, if I were to ask you to “convert Celsius to Fahrenheit”, for the temperature where $C = 50$, you would compute $F = \frac{9 \cdot 50}{5} + 32$, and tell me the resulting value of $F$—namely, $122$.

(This is an example of an invocation of an already defined operation.)

Terminology

method

A named, high-level operation.

In some programming languages, a named operation is called a subroutine, a procedure, or a function. In object-oriented languages, we may distinguish between functions that are not defined as part of a class or object definition, and member functions or methods which are part of a class or object definition. In Java, all code is contained within class definitions (or interface definitions; however, interfaces are not in-scope for this module), and we call these named operations methods.

parameter

A named input declared as part of the definition of a method.

When defining a method in Java, every parameter must be declared with an explicit type. In the conceptual example above, $C$ is a parameter, presumed to be a number. It’s not clear from the example whether $C$ must be an integer, with no fractional part, or possibly a real number (with zero or more digits following a decimal point); in fact, the formula holds for any real number or integer.

argument

A value used in the invocation of a method, corresponding to a parameter in the method definition.

In Java, the arguments in a method invocation must match (or at least be compatible with) the parameters in the method definition, in type, order, and number. When the instructor invoked the “convert Celsius to Fahrenheit” operation, the argument provided was the value $50$.

return type

The type of value a method is defined to return as its result.

Every Java method definition must include the declaration of a return type—or, if the method is not intended to return a result, the keyword void must be used to indicate that there is no return value. If a non-void return type is declared, then the method must return a value of that type.

In the example above, there wasn’t really an explicit declaration of a return type. However, since any real number, when multiplied by 9, divided by 5, then added to 32, results in a real number, we can infer that the return type is also real. To reduce the possibility of a common type of programming error, Java requires us to declare the return type explicitly when we define a method.

method declaration

The portion of a method definition where the method name, return type, and parameter names and types are stated.

Note that the declaration doesn’t include any details of the actual operation being performed by the method. (Of course, it’s almost always a good idea to name the method and its parameters in such a way that the purpose of a method is clear, even if the details of how it fulfills that purpose aren’t.)

method implementation

The body of the method definition, stating the actual operations that will be performed when the method is invoked.

In general, the method implementation includes Java statements that reference the parameters, performing a sequence of lower-level operations on them in order to execute the intended high-level operation, and to return a value of the declared return type (if any).

  1. The arithmetically inclined reader has probably already noticed that this is could be expressed equivalently as $F = 1.8C + 32$. We’ll discuss this and other simplifications when we continue with this example in “Definition”