Methods in Java Classes: Defining a Method

What is the structure of a method definition?

Overview

The rules for defining a method in Java are very strict; in fact, they’re fairly strict in most programming languages, but Java is more strict than most. It takes some work to memorize the details—but as we begin to master them, we can begin to understand any Java methods we read, and start writing our own methods.

As noted in “Introduction: Terminology”, a method definition actually consists of two parts, the declaration and the implementation. Actually, this is the case for concrete methods, but not for abstract methods, indicated by the abstract modifier on a method declared in a class. Abstract methods are discussed a bit more in “Modifiers”.

The syntax and example below identify the declaration and implementation parts of a method definition, and the elements of each.

Syntax

Placeholders are indicated in italics, with optional elements in italicized square brackets.

[modifiers] returnType methodName([parameterType parameterName[, …]]) [throws exceptionType[, …]] {
  [statements]
}

Placeholders

Method declaration

modifiers

Java methods may be declared with zero or more modifiers; these primarily determine the context and visibility of the method—that is, specifying whether the method will be executed in the context of the enclosing class as a whole, or in the context of individual instances of the class; and specifying whether (and to what extent) the method will be visible (and invokable) outside the enclosing class.

Though a comprehensive introduction to all of the possible modifiers is not within the scope of this module, the following are introduced in the “Modifiers” page that follows:

  • Access-level modifiers: private, protected, public
  • Context modifier: static
  • Concrete vs. abstract modifier: abstract
returnType

The type of value that the method will return as a result of its execution, if any. This must match the declared name of that type exactly. For example, if a method is intended to return a value of the primitive type int, then the return type must be written as int, not Int, INT, etc. Similarly, if a method is intended to return a value of the class (object type) String, then the return type must be written as String, not string.

If a method is not intended to return a value, void is used—in place of a return type—to indicate this. Note that though we might say (a bit sloppily) that some method “has a void return type”, void isn’t actually a Java type itself, but indicates (only) that a method does not return a value.

methodName

The name of the method; this is nearly always written in lowerCamelCase.

parameterType parameterName

The type of an input parameter value (spelled and cased exactly according to the declared name of the type), along with the name by which that value is referenced in the method implementation. Both of these must be specified, for each parameter included in the definition.

As a rule, the name of a parameter is spelled in lowerCamelCase.

Even if there are no parameters in the method definition, the enclosing parentheses must be included.

exceptionType

The type of exception that the method is capable of throwing. There may be zero or more of these; if there are none, then the throws keyword must not be included in the method declaration. Any of these are specified by class name, so they will virtually always be written in UpperCamelCase.

Method implementation

statements

The sequence of Java statements that must be executed in order to complete the higher-level operation that is the method’s purpose. If the method has a non-void return type, this sequence must include at least one return returnValue; statement, where returnValue is an expression giving the value returned by the method.

Example

Continuing with the temperature conversion example, we might define a method that performs the Celsius-to-Fahrenheit conversion as follows:

double convertC2F(double c) {
  double f = 9 * c / 5 + 32;
  return f;
}

There are a few things worth noting about this example:

Alternative definition

These two simplifications can be combined:

double convertC2F(double c) {
  return (1.8 * c + 32);
}

Note that the parentheses in the expression (1.8 * c + 32) aren’t required; they’re used here to make it clear (e.g. to a reader who might not be very conversant with Java syntax) that it is the computed value of the expression that is returned by the convertC2F method.

(In case you’re curious, the second form is more efficient at runtime—not because the 2 lines have been collapsed into 1, but because the combination of one floating-point multiplication and one floating-point division has been replaced by one floating-point multiplication.)