Detailed summary of Java naming rules

Source: Internet
Author: User
Tags naming convention

The class name should be the first letter capitalized noun. The naming should be concise and descriptive. The name of the exception class should end with exception. Interface naming rules are the same as class

1. The Java source file name must be the same as the class name of the class defined in the source file.

2. the first part of the package naming package name should be a lowercase ASCII character and be one of the top-level domain names, usually com, edu, gov, mil, net, org, or a national unique flag code defined by ISO standard 3166, 1981. The subsequent parts of the package name are determined by the organization's internal naming conventions, which specify the directory name of each component, the department name, the project name, and so on.

3. The named Class name of the Class/interface should be the first capitalized noun. The naming should be concise and descriptive. The name of the exception class should end with exception. The interface naming convention is the same as class.

4. The letters of a constant named constant name should all be capitalized, the different words are connected by an underscore, and the name combination should give meaning.

5. Naming of variables

5.1 Normal variable the first letter of the ordinary variable name is lowercase, and the first letter of each other word is capitalized. It should be named with a brief and specific meaning, with a clear and concise presentation of its intended use to the user.

5.2 Convention variables, called contract variables, are those temporary variables that can be discarded (throwaway) after they are used. Usually I, J, K, M, and n represent integer variables, and C, D, and e represent character variables.

6. The first word of the method's named method name should be a verb, and the first letter is lowercase, and the first letter of each word is capitalized.

7. Naming the method parametersYou should select a meaningful name as the parameter name for the method. If possible, select the same name as the field you want to assign the value to.
Additional information:1. The first letter of the class name should be capitalized. The first letter of an attribute (member variable), method, object variable, and all identifiers (such as formal arguments, actual arguments, local variables) should be lowercase, and all words contained in it should be enclosed together, with the first letter of the middle word capitalized. For example:Class Name: Thisisaclassname property or method Name: Thisismethodorfieldname object variable: thisisaclassvariable
2. Java package is a special case, they are all lowercase letters, even if the middle of the word is also true. For the global package, reverse your Internet domain name and add the package name. For example:Cn.edu.usst.dingyuewei.package.In addition, the package line is to be preceded by the import line, and the standard pack name in import is preceded by the local package name, and is sorted alphabetically. If the import row contains a different subdirectory in the same package, it should be handled with *. For example:
Package hotlava.net.stats;Import java.io.*;Import java.util.Observable;Import hotlava.util.Application;
Here java.io.* is used instead of InputStream and OutputStream.
3. Interface (Interface): Use full English descriptors to describe the interface encapsulation, the first letter of all words capitalized. Traditionally, the name is appended with the suffix able, ible or ER. For example: Contactable,prompter.
4, the component (Component) uses the complete English description to explain the use of the component, the end Bell the component type. For example: Okbutton,customerlist,filemenu.
5, the name of common methods in the class:
A) The method of obtaining a class (typically with a return value) is generally required to be accessed by the method name using the field name, preceded by a prefix get, such as Getfirstname (), Getlastname ().
b) The method of judging the Boolean type of the class generally requires that the method name be prefixed with the word is, such as ispersistent (), isstring (). Or use words that have logical meanings, such as equal or equals
c) The method of setting the class (the general return type is void): prefix the accessed field name with set, such as Setfirstname (), Setlastname (), Setwarpspeed ()
D) General methods of the class generally use the full English description of the member method function, the first word as far as possible to use a vivid verb, the first letter lowercase, such as openFile (), AddAccount ().
e) The construction method should be written in an incremental way (for example: the number of arguments is written in the back). For example: Public CounterSet () {} public counterset (int size) {this.size = size;}
F) ToString method: In general, each class should define the ToString method in the format: public String tostring () {...}
g) You should generally consider placing a main () method that contains the code used to test that class, and if it contains the main () method, it should be written at the bottom of the class.
6. Static constant fields are generally all uppercase letters, and the words are separated by underscores (there are exceptions, such as the Java class Library about color constants are not strictly all uppercase letters). such as Min_balance,default_date.
7. Cyclic counting variables usually use letters I,J,K or counter. The array should always be named in the following way: objecttype[] or byte[] buffer.

Detailed summary of Java naming rules

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: info-contact@alibabacloud.com 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.