Java Reference
In-Depth Information
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 ad-
ditional specific conventions for method names:
• Methods to get and set an attribute that might be thought of as a variable
V
should be named
get
V
and
set
V
. An example is the methods
getPriority
and
setPriority
of class
Thread
.
• A method that returns the length of something should be named
length
, as in
class
String
.
• A method that tests a boolean condition
V
about an object should be named
is
V
. An example is the method
isInterrupted
of class
Thread
.
• A method that converts its object to a particular format
F
should be named
to
F
. Examples are the method
toString
of class
Object
and the methods
toLocaleString
and
toGMTString
of class
java.util.Date
.
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 Java SE platform
API, will make it easier to use.
Field Names
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 classes
have very few
public
or
protected
fields, except for fields that are constants (
static final
fields).
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
and
the
field
of
the
class
bytesTransferred
java.io.InterruptedIOException
.
Constant Names
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 abbrevi-
ations, all uppercase, with components separated by underscore “
_
” characters. Con-
stant 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
Character
.