0% found this document useful (0 votes)
16 views5 pages

Naming Conventions

Uploaded by

ht79247
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views5 pages

Naming Conventions

Uploaded by

ht79247
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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.
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:

public class Fish {...}

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


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 locate() {...} // verb


public String getWayPoint() {...} // verb and noun

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 wayPoint;

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 printHotSpots(ArrayList spotList) {


int counter = 0;
for (String hotSpot : spotList) {
System.out.println("Hot Spot #"
+ ++counter + ": " + hotSpot);
}
}

Temporary variable names may be single letters such as i, j, k, m, and n for integers and c, d,
and e for characters.

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);
}

Constant Names

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

public static final int MAX_DEPTH = 200;

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

enum Battery {CRITICAL, LOW, CHARGED, FULL}

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:

public String getGpsVersion() {...}


9 - Naming Conventions - Oracle
Naming conventions make programs more understandable by making them easier to read. They can also give information about the
function of the identifier-for example, whether it's a constant, package, or class-which can be helpful in understanding the code.
Link: https://www.oracle.com/java/technologies/javase/codeconventions-namingconventions.html

Iden�fier Type Rules for Naming Examples

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;

You might also like