Java Naming conventions

Source: Internet
Author: User
Tags comment tag uppercase letter

The purpose of defining the specification is to unify the code style of the project so that the program is readable.

Name of the project (all lowercase)

All lowercase, such as CMS, Workdesk,jobserver, etc.

Name of package (all lowercase, defined by domain name)

The Java package name is made up of lowercase words. However, due to the Java object-oriented programming feature, each Java programmer can write their own Java package, in order to guarantee the uniqueness of each Java package naming, in the latest Java programming specification, the programmer is required to add a unique prefix to the name of the package they define. Because domain names on the Internet are not duplicated, programmers generally use their own domain name on the Internet as the only prefix for their packages.
Example: Net.frontfree.javagroup

Name of the class (capitalize the first letter of the word)

By convention, the Java class name usually begins with an uppercase letter, and if the class name consists of multiple words, the first letter of each word should be capitalized such as Testpage, and if the class name contains a word abbreviation, each letter of the word should be capitalized, such as: Xmlexample, One more naming technique is that because a class is designed to represent an object, you should choose a noun when you name the class.
For example: Graphics

Name of method (first letter lowercase, letter start uppercase)

The first word of the name of the method should begin with a lowercase letter, and the following words begin with a capital letter.
Example: DrawImage

Name of the constant (all uppercase, often underlined)
The names of the constants should all use uppercase letters and indicate the full meaning of the constant. If a constant name consists of multiple words, you should use underscores to split the words.
Example: Max_value

Name of the parameter

The naming conventions for parameters are the same as the naming conventions for methods, and to avoid confusion when reading a program, make the name of the parameter as clear as possible if the parameter name is a single word.

Javadoc notes

In addition to the common annotations that Java can use, the Java language Specification defines a special kind of comment, which is what we call the Javadoc annotation, which is used to record the API in our code. Javadoc comments are a multiline comment that begins with/** and ends with a comment that can contain some HTML tags and specific keywords. The advantage of using the Javadoc annotation is that comments written can be automatically converted to online documents, eliminating the hassle of writing program documents separately.
For example:

* This is an example of
* Javadoc
* @author Darchon
* @version 0.1, 10/11/2002

At the beginning of each program, generally use Javadoc annotation to the overall description of the program and copyright information, and then in the main program for each class, interface, method, field to add Javadoc comments, the beginning of each note in a sentence to summarize the class, interface, methods, fields completed functions, This sentence should occupy a single line to highlight its general role, in the following sentence can follow a more detailed description of the paragraph. After a descriptive paragraph, you can also follow some special paragraphs that start with the Javadoc comment tag, such as the @auther and @version in the example above, which appear in the generated document in a specific way.

Variable and constant naming

Name of the variable

The main naming conventions are the following three types:
Camel notation: The first letter is lowercase and the next word starts with a capital letter
Pascal notation: The first letter is capitalized and the next word starts with a capital letter
Hungarian notation: Appends a lowercase sequence to a variable in Pascal notation to indicate the type of the variable

In Java we generally use the Hungarian notation, the basic structure of scope_typevariablename, which uses a 1-3-character prefix to represent the data type, 3-character prefixes must be lowercase, the prefix is a word with a strong ideographic words or multiple words of the name, and the first letter of each word capitalized, the other letters lowercase, so that the variable name can be correctly segmented. For example, define an shaping variable that is used to record the number of documents: Intdoccount, where int indicates the type of data, followed by a ideographic English name, capitalized for each word. In this way, a variable name can reflect both the meaning of the variable type and the value stored by the variable, which makes the code statement more readable and easier to understand. byte, int, char, long, float, double, Boolean, and short.

Data type/prefix (attached)
Char C
Short sh
int I
Long L
Char C
string s
Float F
Double D
Hashtable h
[] Arr
List LST
Vector V
StringBuffer SB
Boolean b
Byte BT
Map map
Object ob

For global variables to be used within multiple functions, add "g_" to the front. For example, a global string variable: G_struserinfo.

The following points should be noted when naming variables:

· Select a meaningful name and note that the first letter of each word should be capitalized.

· The same variable is not used in a function to represent two values with different meanings before and after.

· I, j, K, etc. are only used as cyclic index variables for small loops.

· Avoid naming state variables with flag.

· Use is to name logical variables, such as: Blnfileisfound. This gives the Boolean variable a positive form of naming, allowing other developers to understand more clearly what Boolean variables represent.

· If necessary, add a computed qualifier at the end of the variable, such as: Cursalessum.

· The names do not contain, cursales and cursalessum.

· The name of the static final variable (constant) should be capitalized and indicate the full meaning.

· If you need to abbreviate variable names, be sure to note the consistency of the abbreviation rules throughout your code. For example, if you use intcnt in some areas of your code, and you use intcount in other areas, you add unnecessary complexity to your code. It is recommended to avoid abbreviations in variable names.

· By placing a quantifier at the end, you can create more uniform variables that are easier to understand and easier to search. For example, use Strcustomerfirst and strcustomerlast instead of using Strfirstcustomer and Strlastcustomer. The commonly used quantifier suffixes are: first (one in a set of variables), last (the final of a set of variables), next (the next variable in a set of variables), Prev (the previous one in a set of variables), Cur (the current variable in a set of variables).

· Choose the best data type for each variable, which reduces the need for memory, speeds up code execution, and reduces the likelihood of errors. The data type used for the variable may affect the result of the variable being evaluated. In this case, the compiler does not produce a run-time error, it simply forces the value to conform to the data type requirements. This kind of problem is extremely difficult to find.

· Try to narrow the scope of the variable. If the scope of the variable is greater than it should be, the variable can continue to exist, and the resource is still occupied for a long time after the variable is no longer needed. The main problem is that any method in any class can modify them, and it is difficult to keep track of where they were modified. Occupying resources is an important issue involved in the scope. Minimizing the scope of a variable can have a huge impact on the reliability of the application.


method of naming constants, in Java code, advocates applying constants in place of numbers and fixed strings whenever they are used. That is, the program should try to avoid other numbers except 0 and 1. Constants can be concentrated at the beginning of a program's definition or wider scope, the names should all use uppercase letters, and indicate the full meaning of the constant. If a constant name consists of multiple words, you should use the underscore "_" to split the words such as: Num_days_in_week, Max_value

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.