| CHAPTER 6: Names |
Previous |
Java Language |
Index |
Next |
The Java system and standard classes attempt to use, whenever possible, names chosen according to the conventions presented here. These conventions help to make code more readable and avoid certain kinds of name conflicts.
We recommend these conventions for use in all Java programs. However, these conventions should not be followed slavishly if long-held conventional usage dictates otherwise. So, for example, the sin and cos methods of the class java.lang.Math have mathematically conventional names, even though these method names flout Java convention because they are short and are not verbs.
Names of packages that are to be made widely available should be formed as described in S7.7. Such names are always qualified names whose first identifier consists of two or three uppercase letters that name an Internet domain, such as COM , EDU , GOV , MIL , NET , ORG , or a two-letter ISO country code such as UK or JP . Here are examples of hypothetical unique names that might be formed under this convention:
COM.JavaSoft.jag.Oak ORG.NPR.pledge.driver UK.ac.city.rugby.game
Names of packages intended only for local use should have a first identifier that begins with a lowercase letter, but that first identifier specifically should not be the identifier java ; package names that start with the identifier java are reserved to JavaSoft for naming standard Java packages.
When package names occur in expressions:
Names of class types should be descriptive nouns or noun phrases, not overly long, in mixed case with the first letter of each word capitalized. For example:
ClassLoader SecurityManager Thread Dictionary BufferedInputStream
Likewise, names of interface types should be short and descriptive, not overly long, in mixed case with the first letter of each word capitalized. The name may be a descriptive noun or noun phrase, which is appropriate when an interface is used as if it were an abstract superclass, such as interfaces java.io.DataInput and java.io.DataOutput ; or it may be an adjective describing a behavior, as for the interfaces java.lang.Runnable and java.lang.Cloneable .
Hiding involving class and interface type names is rare. Names of fields, parameters, and local variables normally do not hide type names because they conventionally begin with a lowercase letter whereas type names conventionally begin with an uppercase letter.
Method names should be verbs or verb phrases, in mixed case, with the first letter lowercase and the first letter of any subsequent words capitalized. Here are some additional specific conventions for method names:
Whenever possible and appropriate, basing the names of methods in a new class on names in an existing class that is similar, especially a class from the standard Java Application Programming Interface classes, will make it easier to use.
Method names cannot hide or be hidden by other names (S6.5.6).
Names of fields that are not final should be in mixed case with a lowercase first letter and the first letters of subsequent words capitalized. Note that well-designed Java classes have very few public or protected fields, except for fields that are constants (final static fields) (S6.8.5).
Fields should have names that are nouns, noun phrases, or abbreviations for nouns. Examples of this convention are the fields buf , pos , and count of the class java.io.ByteArrayInputStream (S22.6) and the field bytesTransferred of the class java.io.InterruptedIOException (S22.30.1).
Hiding involving field names is rare.
The names of constants in interface types should be, and final variables of class types may conventionally be, a sequence of one or more words, acronyms, or abbreviations, all uppercase, with components separated by underscore "_ " characters. Constant names should be descriptive and not unnecessarily abbreviated. Conventionally they may be any appropriate part of speech. Examples of names for constants include MIN_VALUE , MAX_VALUE , MIN_RADIX , and MAX_RADIX of the class java.lang.Character .
A group of constants that represent alternative values of a set, or, less frequently, masking bits in an integer value, are sometimes usefully specified with a common acronym as a name prefix, as in:
interface ProcessStates {
int PS_RUNNING = 0;
int PS_SUSPENDED = 1;
}
Hiding involving constant names is rare:
Local variable and parameter names should be short, yet meaningful. They are often short sequences of lowercase letters that are not words. For example:
One-character local variable or parameter names should be avoided, except for temporary and looping variables, or where a variable holds an undistinguished value of a type. Conventional one-character names are:
Local variable or parameter names that consist of only two or three uppercase letters should be avoided to avoid potential conflicts with the initial country codes and domain names that are the first component of unique package names (S7.7).
| © 1996 Sun Microsystems, Inc. All rights reserved. |