Home  >  Article  >  Java  >  Java naming convention

Java naming convention

巴扎黑
巴扎黑Original
2016-12-20 17:28:431371browse

In object-oriented programming, naming classes, objects, methods, variables, etc. is very skillful. For example, distinguish between upper and lower case, use different starting letters, etc. But going back to its roots, when naming a resource, it should be named based on the two characteristics of descriptiveness and uniqueness, so as to ensure that there is no conflict between resources and that each one is easy to remember.

Package naming

The names of Java packages are composed of lowercase words. However, due to the characteristics of Java object-oriented programming, every Java programmer can write his own Java package. In order to ensure the uniqueness of each Java package naming, in the latest Java programming specifications, programmers are required to define their own Java package. The name of the package is preceded by a unique prefix. Since domain names on the Internet are not repeated, programmers generally use their own domain name on the Internet as the unique prefix of their own program packages.
 For example: net.frontfree.javagroup

Class naming

 The name of the class must start with a capital letter and the other letters in the word are lowercase; if the class name consists of multiple words, the first letter of each word must be It should be capitalized, such as TestPage; if the class name contains an abbreviation, each letter of the written word should be capitalized, such as: XMLExample. Another naming trick is that because classes are designed to represent objects, so when naming When classifying, try to choose nouns. 
For example: Circle

method naming

 The first word of the method name should start with a lowercase letter, and subsequent words should start with an uppercase letter.
 For example: sendMessge

Naming of constants

  The names of constants should all use uppercase letters, and indicate the complete meaning of the constant. If a constant name consists of multiple words, the words should be separated by underscores.
 For example: MAX_VALUE

Naming of parameters

 The naming convention of parameters is the same as that of methods. In order to avoid confusion when reading the program, please make the naming of parameters as clear as possible while ensuring that the parameter name is one word. .

Javadoc comments

In addition to our common comment methods, Java language specification also defines a special comment, which is what we call Javadoc comment, which is used to record the API in our code . Javadoc comments are multi-line comments that end with /**beginning with*/. The comments can contain some HTML tags and special keywords. The advantage of using Javadoc comments is that the comments written can be automatically converted into online documents, eliminating the trouble of writing separate program documentation.
 For example:

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

 At the beginning of each program, Javadoc comments are generally used to describe the overall description of the program and copyright information. Later, in the main program, Javadoc comments can be added for each class, interface, method, and field. At the beginning of each comment, use one sentence to summarize the functions performed by the class, interface, method, and field. This sentence should occupy a separate line to highlight its summary function. This sentence can be followed by a more detailed description paragraph. The descriptive paragraphs can also be followed by some special paragraphs starting with Javadoc comment tags, such as @auther and @version in the above example. These paragraphs will be displayed in a specific way in the generated document.
 Although adding comments to a poorly designed program will not make it a good program, if the program is written according to programming standards and adding good comments to the program can help you write a perfectly designed, efficient and easy-to-understand program. Programming, especially when multiple people are working together on the same project, becomes even more important. As the saying goes, "Whether you sharpen your sword or chop wood," it pays to spend some time adapting to Java programming standards.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn