Java Reference
In-Depth Information
of a data type and an identifier. For example, the following method sig-
nature declares two parameters, an int and a float:
public float divide(int x, float f)
Parameters and arguments are discussed in detail later in this chapter.
List of thrown exceptions. Methods can throw an exception back to the
caller of the method. An exception is thrown when a problem arises that
the method is unable (or does not want) to handle itself. If a method
throws a checked exception, the exception must be declared in the
signature using the throws keyword. A method can declare multiple
exceptions after the throws keyword, in which case they are separated
by commas. For example, the following method signature declares that
the method throws two possible exceptions:
public void readFromFile() throws IOException, SecurityException
If a method does not need to declare any exceptions using the throws
keyword, this part of the signature is simply left off. Exceptions are
discussed in detail in Chapter 11, “Exception Handling.”
Here are some examples of method signatures:
public int getDay()
private void setName(String f, String g)
int calibrate(double radius, int multiplier, boolean metric)
public void addListener(Listener a) throws TooManyListenersException
public Employee findEmployee(int number) throws SQLException
From these signatures, you can determine everything you need to know
about invoking each of these methods. For example, if you want to invoke get-
Day(), you do not pass in any data and you get back an int. With setName(),
you must pass in two Strings, and nothing is returned.
To invoke the calibrate() method, you must pass in a double, an int, and a
boolean, in that order, and an int will be returned. The addListener() method
takes in a Listener object, returns nothing, and possibly throws a TooMany-
Listeners exception. The findEmployee() method takes in an int, returns a ref-
erence to an Employee object, and possibly throws an SQLException.
Now that you have seen the information that can be obtained by analyzing
the signature of a method, let's look at how data is passed from one method to
another.
Arguments and Parameters
The signature of a method contains a list of parameters , which are used to
declare what type of data needs to be passed in to the method. The term argu-
ment refers to the data that is passed in to a parameter. When a method is
Search WWH ::




Custom Search