20. Coding Conventions

20.3. Naming Conventions

The choice of identifiers for various elements within a program can help improve the readability of the program. Identifiers should be descriptive of the element’s purpose. The name of class should be descriptive of the class’s role or function. The name of a method should be descriptive of what the method does.

The way names are spelled can also help improve a program’s readability. Ta- ble A.1 summarizes the various conventions recommended by the Java Language Specification and followed by professional Java programmers.

 

TABLE A.1 Naming rules for Java identifiers.

 

Identifier Type

Naming Rule

Example

Class

Nouns in mixed case with the first letter of each internal word capitalized.

OneRowNim TextField

Interfaces

Same as class names. Many interface names end with the suffix able.

Drawable ActionListener

Method

Verbs in mixed case with the first letter in lowercase and the first letter of internal words capitalized.

actionPerformed() sleep() insertAtFront()

Instance Variables

Same as method names. The name should be descriptive of how the variable is used.

maxWidth isVisible

Constants

Constants should be written in uppercase with internal words separated by .

MAX LENGTH XREF

Loop Variables

Temporary variables, such as loop variables, may have single character names: i, j, k.

int k; int i;