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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

NAMES Method Names 6.8.4<br />

DISCUSSION<br />

If a generic method appears inside a generic class, it’s a good idea to avoid using the same<br />

names for the type parameters of the method and class, to avoid confusion. <strong>The</strong> same<br />

applies to nested generic classes.<br />

DISCUSSION<br />

<strong>The</strong>se conventions are illustrated in the code snippets below:<br />

public class HashSet extends AbstractSet { ... }<br />

public class HashMap extends AbstractMap { ... }<br />

public class ThreadLocal { ... }<br />

public interface Functor {<br />

T eval() throws X;<br />

}<br />

When type parameters do not fall conveniently into one of the categories mentioned,<br />

names should be chosen to be as meaningful as possible within the confines<br />

of a single letter. <strong>The</strong> names mentioned above (E , K, T, V, X) should not<br />

be used for type parameters that do not fall into the designated categories.<br />

6.8.4 Method Names<br />

Method names should be verbs or verb phrases, in mixed case, with the first letter<br />

lowercase and the first letter of any subsequent words capitalized. Here are some<br />

additional specific conventions for method names:<br />

• Methods to get and set an attribute that might be thought of as a variable V<br />

should be named getV and setV. An example is the methods getPriority<br />

and setPriority of class Thread.<br />

• A method that returns the length of something should be named length,asin<br />

class String.<br />

DRAFT<br />

• A method that tests a boolean condition V about an object should be named<br />

isV. An example is the method isInterrupted of class Thread.<br />

149

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

Saved successfully!

Ooh no, something went wrong!