|
|
Language Fundamentals - Method Declarations
Syntax (JJ pg88)
modifiers returnValue methodName(parameterList)
throwsClause
{
// Method body
}
The modifiers and throwsClause are optional.
Modifiers
returnValue (JLS §8.4.5)
- legal return types: void, any primitive data type, an Object reference or Array type
- if void is used, the method may not use a return statement with an expression
return; // legal
return(x); // illegal
- if a primitive data type is used, the method must return a value that is promotable to the declared type
- if an array type is used, the method must return a value of the same array type.
For example, if the returnType is String[][] then the method must return a String[][] array
- a method can declare a return type without having a return statement in its body
class DizzyDean {
int pitch() { throw new RuntimeException("90 mph?!"); }
}
parameterList
throwsClause
- consists of the keyword throws and a comma-separated list of the exceptions that may be thrown
- identifies all the checked exceptions that may be thrown but not caught by the method
- the throws clause must include exceptions that may be thrown by another method being invoked by the declared method
- it is not necessary to throw the predefined exceptions which are subclasses of the Error or RuntimeException classes (JLS §8.4.4)
- a method that overrides another method cannot be declared to throw more checked exceptions than the method being overidden.(JLS §
8.4.4)
class classA {
void methodA() throws exX, exY{
// method body
}
}
class classB extends classA {
void methodA() throws exX { // can throw less exceptions
// method body
}
}
class classC extends classA {
void methodA() throws exX, exY, exZ { // illegal
// method body
}
}
Method Signature
- A method signature is made up of the method name and parameter list (it does not include the return type)
- it is illegal for a class to declare two methods with the same signature
Method body
- a static method cannot use this or super operators in it's body (static implies a class method unrelated to any specific instance) (JLS §8.4.3.2)
- a method declared native or abstract has a semi-colon (;) for a body. Do not use curly braces {}.
(JLS §8.4.5)
Example of native and abstract method declarations:
public native void close() throws IOException;
public abstract void open() throws IOException;
versus non-native or abstract method declaration:
public void close() throws IOException {
// Method body
}
- if a method is declared void then the body should not include a return statement that has an expression (JLS §8.4.5)
public void methodA() {
return( 1 + 1 ); // illegal
}
public void methodA() {
return; // legal
}
Also see
Understanding that parameters are passed by value and not by reference
Code Examples
Tips
- any method can throw a Runtime or Error exception without declaring it in the throws clause
- methods having the same name and parameter types do not have the same signature unless the parameter types are listed in the same order
Traps
- an abstract method also declared private, native, final, synchronized, or strictfp
- an abstract method declared in a non-abstract class
- a native or abstract method with a method body
- method returning a type which is not convertible to the declared return type
- a void method returning a value
- a static method referencing this or super
|