Java Naming conventions

Source: Internet
Author: User
Tags class definition naming convention

Java Naming Conventions

With regard to the naming of various elements in Java, the purpose of defining these specifications is to make all documents in the project look like one person, increasing readability and reducing the loss of the project team due to substitutions. (These specifications do not have to be absolutely adhered to, but make sure that the program is readable):

    1. naming of the package
      The package name should be made up of a lowercase word.

    2. Name of Class
      Class names must consist of words that begin with uppercase letters and other lowercase letters.

    3. Name of Class variable
      The name of the variable must begin with a lowercase letter. The following words start with capital letters.

    4. Name of the Static Final variable
      The name of the Static Final variable should be capitalized and indicate the full meaning.

    5. Name of the parameter
      The name of the parameter must be the same as the variable's naming convention.

    6. Naming the array
      Arrays should always be named in the following way:
      1 byte   
      Instead of:
      1 byte buffer[];   
    7. method
      name with meaningful parameters, if possible, with the same names as the fields to be assigned:
       1  Setcounter (int   size) { 2  this . Size = size;  3 } 
    8. Java file style
      all Java (*.java) files must adhere to the following style rules
      Copyright information must be at the beginning of the Java file, such as:
       1  /**  2   * Copyright 0?3 Shanghai XXX Co., Ltd.  3   * all right reserved.  4  */ 
      Other information that does not need to appear in Javadoc can also be included here.
    9. package/imports
      package line to the import line, the standard package name in import is preceded by the local package name, and is sorted alphabetically.
      If the import row contains different subdirectories in the same package, it should be handled with *.
       1  package   hotlava.net.stats;  2  import  java.io.*  3  import   java.util.Observable;  4  import  hotlava.util.Application; 
      here java.io.* used instead of InputStream and OutputStream.
    10. Class
      the next is the comment of the class, which is usually used to interpret the class.
      1 /** 2 * A class representing a set of packet and byte counters 3 * It is observable-allow it to be watched 4 * Reports changes when the current set was complete 5 */      
      Next is the class definition, which contains the extends and implements of the different rows .
      1  Public class CounterSet 2 extends Observable 3 Implements cloneable 4         
      Next is the member variable for the class:
      1 /** 2 * Packet Counters 3 */ 4 protected int         
      the member variable for public must generate a document (JAVADOC). Member variables defined by the proceted, private, and package can have no comment if the name has a clear meaning.
    11. access Method
      Next is the method of accessing the class variable. It is simply used to get the value of the class variable assignment, which can be simply written on one line.
      1     /**2 * Get the counters3 *@returnAn array containing the statistical data. This array has been4 * Freshly allocated and can is modified by the caller.5     */6      Public int[] Getpackets () {returnCopyarray (packets, offset);}7      Public int[] GetBytes () {returncopyarray (bytes, offset);}8      Public int[] Getpackets () {returnpackets;}9      Public voidSetpackets (int[] packets) { This. packets = Packets; }
      Other methods do not write on a line
    12. constructor function
      Next is the constructor, which should be written in an incremental way (for example: many of the arguments are written in the back).
      The type of access ("public", "private", etc.) and any "static", "final" or "synchronized" should be on one line, and the methods and parameters are written in another line, which makes the methods and parameters easier to read.
      1  Public 2 counterset (int  size) {3this. size = size; 4 }      
    13. Cloning Method
      if this class can be cloned, then the next step is the Clone method:
      1      Public2 Object Clone () {3     Try {4CounterSet obj = (counterset)Super. Clone ();5Obj.packets = (int[]) Packets.clone ();6Obj.size =size;7     returnobj;8}Catch(clonenotsupportedexception e) {9     Throw NewInternalerror ("Unexpected clonenotsupportedexception:" +e.getmessage ());Ten } One}
    14. class Method
      here's how to start writing classes:
      1     /**2 * Set the packet counters3 * (such as when restoring from a database)4     */5     protected Final6     voidSetArray (int[] R1,int[] R2,int[] R3,int[] R4)7     throwsillegalargumentexception8 {9     //ensure the arrays www.gzlij.com is of equal sizeTen if(r1.length! = R2.length | | r1.length! = R3.LENGTH | | R1.length! =r4.length) One     Throw Newillegalargumentexception ("Arrays must be of the the same size"); ASystem.arraycopy (R1, 0, R3, 0, r1.length); -System.arraycopy (R2, 0, R4, 0, r1.length); -}
    15. ToString Method
      in any case, each class should define the ToString method:
      1      Public2 String toString () {3String retval ="CounterSet:";4      for(inti = 0; I < data.length (); i++) {5retval + =data.bytes.toString ();6retval + =data.packets.toString ();7 }8     returnretval;9 }Ten}
    16. Main method
      If the main (string[]) method is already defined, it should be written at the bottom of the class.

Java Naming conventions

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.