Detailed summary of Java naming rules

  • 2020-04-01 02:23:02
  • OfStack

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

2. Name of Package The first part of the 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 country unique identifier defined by ISO standard 3166, 1981. The subsequent part of the Package name is determined by each organization's internal naming rules, which specify the directory name of each component, the department name, the project name, and so on.      

3. Name of Class/Interface Class names should be capitalized nouns. Name it succinctly and descriptive. The name of the Exception class should end with Exception. Interface has the same naming convention as Class.

4. Naming constants Constant names should all be capitalized, different words should be linked by underscores, and name combinations should have meaning.

5. Variable naming

5.1 ordinary variables The first letter of a normal variable name is lowercase, and the first letter of every other word is uppercase. The name should be short and specific meaning, concise and clear to the user to show its use intention.        

5.2 contract variables A convention variable is a temporary variable that is throwaway after being used. Usually I, j, k, m, and n represent integer variables; C, d, and e represent character variables.

6. Method naming The first word of the method name should be a verb with a lowercase first letter and every other word capitalized.    

7. Name of method parameters A meaningful name should be selected as the parameter name of the method. If possible, choose the same name as the field you want to assign.  

Supplementary information:

1. Class names should be capitalized. The first letters of attributes (member variables), methods, object variables, and all identifiers (such as formal parameters, actual parameters, and local variables) should be lowercase, and all the words contained in them should be close together and the first letter of the middle word should be capitalized. Such as:
ThisIsAClassName attribute or method name: thisIsMethodOrFieldName object variable: thisIsAClassVariable

2. Java packages are a special case where they are all lowercase, even the middle word. For the global package, turn your Internet domain name upside down and attach the package name. Such as:
Cn.edu.usst.dingyuewei.package.
In addition, the package line should precede the import line, and the standard package names in import should precede the local package names and be arranged alphabetically. If the import line contains different subdirectories in the same package, it should be handled with *. Such as:

Package hotlava.net.stats;
Import the Java. IO. *;
Import the Java. Util. Observables.
The import hotlava. Util. Application;

Here java.io.* is used instead of InputStream and OutputStream.

3, Interface: the use of a complete English descriptor Interface encapsulation, all words of the first letter uppercase. Traditionally, names are followed by suffixes able, ible, or er. For example, Contactable, Prompter.

Use a complete English description to describe the purpose of the Component, with the Component type at the end. For example: okButton, customerList, fileMenu.

5. Naming of commonly used methods in the class:

A) the method of class acquisition (generally with return value) generally requires the method name to use the name of the field to be accessed, prefixed by get, such as getFirstName(), getLastName().

B) the Boolean method of class generally requires method names to be prefixed with the word is, such as isPersistent () and isString (). Or use words that make logical sense, such as equal or equals

C) set method of class (generally return type is void) : prefix set before the name of the accessed field, such as setFirstName(),setLastName(), setWarpSpeed()

D) the general methods of the class generally use full English description to explain the function of the member method, the first word as far as possible to use a vivid verb, the first letter lowercase, such as openFile(), addAccount().

E) constructors should be written incrementally (e.g. Such as:
Public CounterSet () {}
Public CounterSet(int size){this.size = size; }

F)toString method: in general, every class should define the toString method, its format is: public String toString() {... }

G) you should generally consider putting a main() method that contains the code to test that class, and if the main() method is included, it should be written at the bottom of the class.

6. Static constant fields (static final) are generally all capitalized, and words are separated by underscores (there are exceptions, such as the fact that the constants of colors in the Java class library are not strictly capitalized). For example, MIN_BALANCE DEFAULT_DATE.

7. The loop count variable is usually the letter I, j, k or counter. Arrays should always be named as objectType[] or byte[] buffer.


Related articles: