10.12.2012 Views

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CLASSES Method Body 8.4.7<br />

Object doPrivileged(PrivilegedExceptionAction action) throws E<br />

{ ... }<br />

}<br />

class Test {<br />

public static void main(String[] args) {<br />

try {<br />

AccessController.doPrivileged(<br />

new PrivilegedExceptionAction() {<br />

public void run() throws FileNotFoundException<br />

{... delete a file ...}<br />

});<br />

} catch (FileNotFoundException f) {...} // do something<br />

}<br />

}<br />

8.4.7 Method Body<br />

A method body is either a block of code that implements the method or simply a<br />

semicolon, indicating the lack of an implementation. <strong>The</strong> body of a method must<br />

be a semicolon if and only if the method is either abstract (§8.4.3.1) or native<br />

(§8.4.3.4).<br />

MethodBody:<br />

Block<br />

;<br />

A compile-time error occurs if a method declaration is either abstract or<br />

native and has a block for its body. A compile-time error occurs if a method declaration<br />

is neither abstract nor native and has a semicolon for its body.<br />

If an implementation is to be provided for a method declared void, but the<br />

implementation requires no executable code, the method body should be written<br />

as a block that contains no statements: “{ }”.<br />

If a method is declared void, then its body must not contain any return<br />

statement (§14.17) that has an Expression.<br />

If a method is declared to have a return type, then every return statement<br />

(§14.17) in its body must have an Expression. A compile-time error occurs if the<br />

body of the method can complete normally (§14.1).<br />

In other words, a method with a return type must return only by using a return<br />

statement that provides a value return; it is not allowed to “drop off the end of its<br />

body.”<br />

DRAFT<br />

223

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!