Naming of the package
The package name should be made up of a lowercase word.
Name of Class
Class names must be made up of uppercase letters and other lowercase words.
Name of Class variable
The name of the variable must begin with a lowercase letter. The following words start with capital letters.
Name of the Static Final variable
The name of the Static Final variable should be capitalized and indicate the full meaning.
Name of the parameter
The name of the parameter must be the same as the variable's naming convention.
Naming the array
Arrays should always be named in the following way:
byte[] buffer;
Instead of:
BYTE buffer[];
Parameters of the method
Use meaningful parameters to name them, and, if possible, use the same names as the fields to be assigned:
Setcounter (int size) {
this.size = size;
}
Java File Styles
All Java (*.java) files must adhere to the following style rules
Copyright information
The copyright information must be at the beginning of the Java file, such as:
/**
* Copyright? 0?3 Shanghai XXX Co. Ltd.
* All right reserved.
*/
Other information that does not need to appear in Javadoc can also be included here.
Package/imports
Before the package line is to be import, the standard package name in import is preceded by the local pack name and is sorted alphabetically. If the import row contains a different subdirectory in the same package, it should be handled with *.
Package hotlava.net.stats;
Import java.io.*;
Import java.util.Observable;
Import hotlava.util.Application;
Here java.io.* used instead of InputStream and OutputStream.
Class
The next is the comment of the class, which is usually used to interpret the class.
/**
* A class representing a set of packet and byte counters
* It is observable-allow it to be watched
* Reports changes when the current set was complete
*/
Next is the class definition, which contains the extends and implements of the different rows.
public class CounterSet
Extends Observable
Implements Cloneable
Class fields
Next is the member variable for the class:
/**
* Packet Counters
*/
protected int[] packets;
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.
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.
/**
* Get the counters
* @return An array containing the statistical data. This array has been
* Freshly allocated and can is modified by the caller.
*/
Public int[] Getpackets () {return Copyarray (packets, offset);}
Public int[] GetBytes () {return Copyarray (bytes, offset);}
Public int[] Getpackets () {return packets;}
public void Setpackets (int[] packets) {this.packets = packets;}
Other methods do not write on a line
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.
Public
CounterSet (int size) {
this.size = size;
}
Cloning method
If this class can be cloned, then the next step is the Clone method:
Public
Object Clone () {
try {
CounterSet obj = (counterset) super.clone ();
Obj.packets = (int[]) packets.clone ();
obj.size = size;
return obj;
}catch (Clonenotsupportedexception e) {
throw new Internalerror ("Unexpected clonenotsupportedexception:" + e.getmessage ());
}
}
Class method
Here's how to start writing classes:
/**
* Set the packet counters
* (such as when restoring from a database)
*/
Protected final
void SetArray (int[] R1, int[] R2, int[] R3, int[] R4)
Throws IllegalArgumentException
{
Ensure the arrays www.gzlij.com is of equal size
if (r1.length! = R2.length | | r1.length! = R3.LENGTH | | r1.length! = r4.length)
throw new IllegalArgumentException ("Arrays must be of the same size");
System.arraycopy (R1, 0, R3, 0, R1.length);
System.arraycopy (R2, 0, R4, 0, r1.length);
}
ToString method
In any case, each class should define the ToString method:
Public
String toString () {
String retval = "CounterSet:";
for (int i = 0; i < data.length (); i++) {
retval + = data.bytes.toString ();
retval + = data.packets.toString ();
}
return retval;
}
}
Main method
If the main (string[]) method is already defined, it should be written at the bottom of the class.
Naming conventions for Java