A method body is either a block of code that implements the method or simply a
semicolon, indicating the lack of an implementation. The body of a method must
be a semicolon if and only if the method is either abstract
(§8.4.3.1) or native
(§8.4.3.4).
MethodBody:
Block
;
A compile-time error occurs if a method declaration is either abstract
or
native
and has a block for its body. A compile-time error occurs if a method declaration is neither abstract
nor native
and has a semicolon for its body.
If an implementation is to be provided for a method but the implementation requires no executable code, the method body should be written as a block that contains no statements: "{ }
".
If a method is declared void
, then its body must not contain any return
statement (§14.15) that has an Expression.
If a method is declared to have a return type, then every return
statement (§14.15) in its body must have an Expression. A compile-time error occurs if the body of the method can complete normally (§14.1). In other words, a method with a return type must return only by using a return statement that provides a value return; it is not allowed to "drop off the end of its body."
Note that it is possible for a method to have a declared return type and yet contain no return statements. Here is one example:
class DizzyDean {
int pitch() { throw new RuntimeException("90 mph?!"); }
}