A method is a named collection of Java statements that can be invoked by other Java code. When a method is invoked, it is passed zero or more values known as arguments. The method performs some computations and, optionally, returns a value. A method invocation is an expression that is evaluated by the Java interpreter. Because method invocations can have side effects, however, they can also be used as expression statements.
You already know how to define the body of a method; it is simply an arbitrary sequence of statements enclosed within curly braces. What is more interesting about a method is its signature. The signature specifies:
The name of the method
The type and name of each of the parameters used by the method
The type of the value returned by the method
The exception types the method can throw
Various method modifiers that provide additional information about the method
A method signature defines everything you need to know about a method before calling it. It is the method specification and defines the API for the method. The reference section of this book is essentially a list of method signatures for all publicly accessible methods of all publicly accessible classes of the Java platform. In order to use the reference section of this book, you need to know how to read a method signature. And, in order to write Java programs, you need to know how to define your own methods, each of which begins with a method signature.
A method signature looks like this:
modifiers type name ( paramlist ) [ throws exceptions ]
The signature (the method specification) is followed by the method body (the method implementation), which is simply a sequence of Java statements enclosed in curly braces. In certain cases (described in Chapter 3, "Object-Oriented Programming in Java"), the implementation is omitted, and the method body is replaced with a single semicolon.
Here are some example method definitions. The method bodies have been omitted, so we can focus on the signatures:
public static void main(String[] args) { ... } public final synchronized int indexOf(Object element, int startIndex) { ... } double distanceFromOrigin() { ... } static double squareRoot(double x) throws IllegalArgumentException { ... } protected abstract String readText(File f, String encoding) throws FileNotFoundException, UnsupportedEncodingException;
modifiers is zero or more special modifier keywords, separated from each other by spaces. A method might be declared with the public and static modifiers, for example. Other valid method modifiers are abstract, final, native, private, protected, and synchronized. The meanings of these modifiers are not important here; they are discussed in Chapter 3, "Object-Oriented Programming in Java".
The type in a method signature specifies the return type of the method. If the method returns a value, this is the name of a primitive type, an array type, or a class. If the method does not return a value, type must be void. A constructor is a special type of method used to initialize newly created objects. As we'll see in Chapter 3, "Object-Oriented Programming in Java", constructors are defined just like methods, except that their signatures do not include this type specification.
The name of a method follows the specification of its modifiers and type. Method names, like variable names, are Java identifiers and, like all Java identifiers, can use any characters of the Unicode character set. It is legal (and sometimes useful) to define more than one method with the same name, as long as each version of the method has a different parameter list. Defining multiple methods with the same name is called method overloading. The System.out.println() method we've seen so much of is an overloaded method. There is one method by this name that prints a string and other methods by the same name that print the values of the various primitive types. The Java compiler decides which method to call based on the type of the argument passed to the method.
When you are defining a method, the name of the method is always followed by the method's parameter list, which must be enclosed in parentheses. The parameter list defines zero or more arguments that are passed to the method. The parameter specifications, if there are any, each consist of a type and a name and are separated from each other by commas (if there are multiple parameters). When a method is invoked, the argument values it is passed must match the number, type, and order of the parameters specified in this method signature line. The values passed need not have exactly the same type as specified in the signature, but they must be convertible to those types without casting. C and C++ programmers should note that when a Java method expects no arguments, its parameter list is simply (), not (void).
The final part of a method signature is the throws clause, which I first described when we discussed the throw statement. If a method uses the throw statement to throw a checked exception, or if it calls some other method that throws a checked exception and does not catch or handle that exception, the method must declare that it can throw that exception. If a method can throw one or more checked exceptions, it specifies this by placing the throws keyword after the argument list and following it by the name of the exception class or classes it can throw. If a method does not throw any exceptions, it does not use the throws keyword. If a method throws more than one type of exception, separate the names of the exception classes from each other with commas.
Copyright © 2001 O'Reilly & Associates. All rights reserved.