Method Headers
We summarize method headers, define the signature of a method, and discuss its use. This material is covered in more detail in the course text, Gries/Gries.
A method declaration consists of a specification (as a comment), a method header, and a body. Here is a method declaration:
/** = "x is in the range 1..50". */
public boolean isLess(int x) {
return x >= 1 && x <= 50;
}
Here is the format of the method header we use in this class for the three kinds of methods in Java, giving information about calls on each kind of method, with a few notes below:
Kind of method | Format of header | A call on a method: |
---|---|---|
procedure | public void <name> ( <parameter-list> ) ; | Is a statement. |
function | public <return-type> <name> ( <parameter-list> ) | Is an expression, which yields a value. |
constructor | public <class-name> ( <parameter-list> ) | Appears in a new-expression or as the first statement of a constructor. |
Notes
- Keyword public may be replaced by private, if you want the method to be accessible only in the class in which it is declared.
- The <name> is any Java identifier. Conventions for it are covered below.
- The <return-type> is the <type> of the value returned by the function. It could be a primitive type or a class type.
- The <parameter-list> contains declarations of the parameters of the method. It is a (possibly empty) sequence of declarations separated by commas. Each declaration has the form <type> <variable-name>.
- The <class-name> is the name of the class in which the constructor is declared.
- If the class in which this method header appears is abstract, then keyword abstract may appear after the access modifier (public or private). This is explained in another section.
- For a function or procedure, place keyword static after the access modifier if the method does not refer directly to an instance variable or instance method of the class. Then, there will be only one copy of the method, and it will be placed in the file-drawer of the class.
Method names
A method name is often written as a sequence of one or more words, like toString, setName, and isLess. The first letter of each word, except the first, is capitalized.
A procedure name is usually a command to do something, e.g. setName, printArea, fixName.
A function name is usually one of:
- a noun phrase that describes the value returned, e.g. sin, sqrt, smallestName;
- the name of an algorithm, e.g. quicksort, selectionSort, binarySearch;
- for a boolean function, a true-false phrase that indicates its value, e.g. isLess, areRed;
- for a getter method, the name of a field preceded by get, e.g. getTitle.
Method signature
The signature of a method consists of its name and the types of its parameters (enclosed in parentheses and separated by commas). For example, the signature of the method declared near the beginning of this webpage is
isLess(int)
In Java, the methods in a class must have different signatures. This means, for example, that one cannot have both a procedure and a function with name isLess and one int parameter. Java constrains methods in this fashion so that, for any method call, there is no ambiguity concerning which method is being called.