O'Reilly logo

Java Pocket Guide, 4th Edition by Robert Liguori, Patricia Liguori

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 1. 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. The following examples are from various Java sources.

Acronyms

When using acronyms in names, only the first letter of the acronym should be uppercase and only when uppercase is appropriate:

// e.g., DNA is represented as Dna
public class GliesianDnaProvider {...}

// e.g., Most Recent Common Ancestor (MRCA) is Mrca
public class MrcaCalculator {...}

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 {}

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:

public class AirDensityCalculator {...}

Constant Names

Constant names should be all uppercase letters, and multiple words should be separated by underscores:

private static final double KELVIN = 273.16;
private static final double DRY_AIR_GAS_CONSTANT = 287.058;
private static final double HUMID_AIR_GAS_CONSTANT = 461.4964;

Enumeration Names

Enumeration names should follow the conventions of class names. The enumeration set of objects (choices) should be all uppercase letters:

public enum MeasurementSystem {
  METRIC, UNITED_STATES_CUSTOMARY, IMPERIAL
}

public enum Study {
  ALL, NON_ENDOGAMOUS, SEMI_ENDOGAMOUS, ENDOGAMOUS
}

public enum RelationshipMatchCategory {
  IMMEDIATE, CLOSE, DISTANT, SPECULATIVE
}

Generic Type Parameter Names

Generic type parameter names should be uppercase single letters. The letter T for type is typically recommended.

The Collections Framework makes extensive use of generics. E is used for collection elements, S is used for service loaders, and K and V are used for map keys and values:

public interface Map <K,V> {
   V put(K key, V value);
}

Instance and Static Variable Names

Instance and static variable names should be nouns and should follow the same capitalization convention as method names:

private String prediction;

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:

public interface Relatable {...}
public interface SystemPanel {...}

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:

public void clear() {...} // verb
public void toString() // preposition and noun
public double getDryAirDensity() {...} // verb, adjective and noun
public double getHumidAirDensity() {...} // verb, adjective and noun

Package Names

Package names should be unique and consist of lowercase letters. Underscores may be used if necessary:

// Gliesian.com (company), JAirDensity (software)
package com.gliesian.jairdensity;

// Gliesian.com (company), FOREX Calculator (software), Utilties
package com.gliesian.forex_calculator.utils;

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.

Module Names

Module names should be the reversed internet domain name with the same guidelines as package names:

module com.gliesian.utils {
}

Parameter and Local Variable Names

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:

public void printPredictions (ArrayList predictions) {
  int counter = 1;
  for (String prediction : predictions) {
    System.out.println("Predictions #" + counter++ + ": " + prediction);
  }
}

Temporary variable names may be single letters such as i, j, k, m, and n for integers and c, d, and e for characters. Temporary and looping variables may be one-character names as shown in Table 1-1.

Table 1-1. Temporary and looping variables
One-character name Type

b

Byte

c

Character

d

Double

e

Exception

f

Float

i, j, or k

Integer

l

Long

o

Object

s

String

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required