Example
Package names
- Package names should be all lower case without underscores or other special characters.
- Package names begin with the reversed authority part of the web address of the company of the developer. This part can be followed a by project/program structure dependent package substructure.
- Don’t use plural form. Follow the convention of the standard API which uses for instance
java.lang.annotation
and not java.lang.annotations
.
- Examples:
com.yourcompany.widget.button
, com.yourcompany.core.api
Class, Interface and Enum Names
- Class and enum names should typically be nouns.
- Interface names should typically be nouns or adjectives ending with …able.
- Use mixed case with the first letter in each word in upper case (i.e. CamelCase).
- Match the regular expression
^[A-Z][a-zA-Z0-9]*$
.
- Use whole words and avoid using abbreviations unless the abbreviation is more widely used than the long form.
- Format an abbreviation as a word if the it is part of a longer class name.
- Examples:
ArrayList
, BigInteger
, ArrayIndexOutOfBoundsException
, Iterable
.
Method Names
Method names should typically be verbs or other descriptions of actions
- They should match the regular expression
^[a-z][a-zA-Z0-9]*$
.
- Use mixed case with the first letter in lower case.
- Examples:
toString
, hashCode
Variables
Variable names should be in mixed case with the first letter in lower case
- Match the regular expression
^[a-z][a-zA-Z0-9]*$
- Further recommendation: Variables
- Examples:
elements
, currentIndex
Type Variables
For simple cases where there are few type variables involved use a single upper case letter.
- Match the regular expression
^[A-Z][0-9]?$
- If one letter is more descriptive than another (such as
K
and V
for keys and values in maps or R
for a function return type) use that, otherwise use T
.
- For complex cases where single letter type variables become confusing, use longer names written in all capital letters and use underscore (
_
) to separate words.
- Examples:
T
, V
, SRC_VERTEX
Constants
Constants (static final
fields whose content is immutable, by language rules or by convention) should be named with all capital letters and underscore (_
) to separate words.
- Match the regular expression
^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$
- Examples:
BUFFER_SIZE
, MAX_LEVEL
Other guidelines on naming
- Avoid hiding/shadowing methods, variables and type variables in outer scopes.
- Let the verbosity of the name correlate to the size of the scope. (For instance, use descriptive names for fields of large classes and brief names for local short-lived variables.)
- When naming public static members, let the identifier be self descriptive if you believe they will be statically imported.
- Further reading: Naming Section (in the official Java Style Guide)
Source: Java Style Guidelines from Oracle