Reasons for using a naming convention include the following:
- To reduce the effort needed to read and understand source code.
- To enable code reviews to focus on more important issues than arguing over syntax and naming standards.
- To enable code quality review tools to focus their reporting mainly on significant issues other than syntax and style preferences.
By using standard Java naming conventions, you make your code easier to read for yourself and for other programmers. Readability of Java program is very important. It indicates that less time is spent to figure out what the code does.
|class name||should start with uppercase letter and be a noun e.g. String, Color, Button, System, Thread etc.|
|interface name||should start with uppercase letter and be an adjective e.g. Runnable, Remote, ActionListener etc.|
|method name||should start with lowercase letter and be a verb e.g. actionPerformed(), main(), print(), println() etc.|
|variable name||should start with lowercase letter e.g. firstName, orderNumber etc.|
|package name||should be in lowercase letter e.g. java, lang, sql, util etc.|
|constants name||should be in uppercase letter. e.g. RED, YELLOW, MAX_PRIORITY etc.|
|Identifier Type||Rules for Naming|
The prefix of a unique package name is always written in all-lowercase ASCII letters and should be one of the top-level domain names, currently com, edu, gov, mil, net, org, or one of the English two-letter codes identifying countries as specified in ISO Standard 3166, 1981.
Subsequent components of the package name vary according to an organization’s own 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 letter of each internal word capitalized. Try to keep your class names simple and descriptive. Use whole words-avoid acronyms and abbreviations (unless the abbreviation is much more widely used than the long form, such as URL or HTML).
Interface names should be capitalized like class names.
Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized.
Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter. Internal words start with capital letters. Variable names should not start with underscore _ or dollar sign $ characters, even though both are allowed.
Variable names should be short yet meaningful. The choice of a variable name should be mnemonic- that is, designed to indicate to the casual observer the intent of its use. One-character variable names should be avoided except for temporary “throwaway” variables. Common names for temporary variables are
n for integers;
e for characters.
The names of variables declared class constants and of ANSI constants should be all uppercase with words separated by underscores (“_”). (ANSI constants should be avoided, for ease of debugging.)
static final int MIN_WIDTH = 4;
static final int MAX_WIDTH = 999;
static final int GET_THE_CPU = 1;
Source: http://www.oracle.com, https://en.wikipedia.org.