Naming Conventions
Naming Conventions
Naming Conventions
Naming conventions are used to make Java programs more readable. It is important to use
meaningful and unambiguous names comprised of Java letters.
Link https://www.oreilly.com/library/view/java-8-pocket/9781491901083/ch01.html
Class Names
Class names should be nouns, as they represent “things” or “objects.” They should be
mixed case (camel case) with only the first letter of each word capitalized, as in the
following:
Interface Names
Interface names should be adjectives. They should end with “able” or “ible” whenever
the interface provides a capability; otherwise, they should be nouns. Interface names
follow the same capitalization convention as class names:
Method Names
Method names should contain a verb, as they are used to make an object take action.
They should be mixed case, beginning with a lowercase letter, and the first letter of each
subsequent word should be capitalized. Adjectives and nouns may be included in
method names:
Instance and static variable names should be nouns and should follow the same
capitalization convention as method names:
private String wayPoint;
Parameter and local variable names should be descriptive lowercase single words,
acronyms, or abbreviations. If multiple words are necessary, they should follow the
same capitalization convention as method names:
Temporary variable names may be single letters such as i, j, k, m, and n for integers and c, d,
and e for characters.
Constant Names
Constant names should be all uppercase letters, and multiple words should be
separated by underscores:
Enumeration Names
Enumeration names should follow the conventions of class names. The enumeration set
of objects (choices) should be all uppercase letters:
Package Names
Package names should be unique and consist of lowercase letters. Underscores may be
used if necessary:
package com.oreilly.fish_finder;
Publicly available packages should be the reversed Internet domain name of the
organization, beginning with a single-word top-level domain name (e.g., com, net, org,
or edu), followed by the name of the organization and the project or division. (Internal
packages are typically named according to the project.)
Package names that begin with java and javax are restricted and can be used only to
provide conforming implementations to the Java class libraries.
Annotation Names
Annotation names have been presented several ways in the Java SE API for predefined
annotation types, [adjective|verb][noun]:
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface FunctionalInterface {}
Acronyms
When using acronyms in names, only the first letter of the acronym should be uppercase
and only when uppercase is appropriate:
The prefix of a unique package name is always writen in all-lowercase ASCII leters and
should be one of the top-level domain names, currently com, edu, gov, mil, net, org, or one
com.sun.eng
of the English two-leter codes iden�fying countries as specified in ISO Standard 3166, 1981.
Packages com.apple.quicktime.v2
Subsequent components of the package name vary according to an organization's own
edu.cmu.cs.bovik.cheese
internal naming conventions. Such conventions might specify that certain directory
name components be division, department, project, machine, or login names.
Class names should be nouns, in mixed case with the first leter of each internal word
capitalized. Try to keep your class names simple and descrip�ve. Use whole words-avoid class Raster;
Classes
acronyms and abbrevia�ons (unless the abbrevia�on is much more widely used than the class ImageSprite;
long form, such as URL or HTML).
interface RasterDelegate;
Interfaces Interface names should be capitalized like class names.
interface Storing;
run();
Methods should be verbs, in mixed case with the first leter lowercase, with the first leter of
Methods runFast();
each internal word capitalized.
getBackground();
Iden�fier Type Rules for Naming Examples
Except for variables, all instance, class, and class constants are in mixed case with a
lowercase first leter. Internal words start with capital leters. Variable names should not start
with underscore _ or dollar sign $ characters, even though both are allowed.
int i;
Variables Variable names should be short yet meaningful. The choice of a variable name should
char c;
be mnemonic- that is, designed to indicate to the casual observer the intent of its use. float myWidth;
One-character variable names should be avoided except for temporary "throwaway"
variables. Common names for temporary variables are i, j, k, m, and n for
integers; c, d, and e for characters.
The names of variables declared class constants and of ANSI constants should be all sta�c final int MIN_WIDTH = 4;
Constants uppercase with words separated by underscores ("_"). (ANSI constants should be avoided,
for ease of debugging.) static final int MAX_WIDTH = 999;
static final int GET_THE_CPU = 1;