The Java Programming Specification Naming Specification Defining This specification is to let all documents in the project look like a person written, increase readability, and reduce the loss caused by the project group. (These norms are not necessarily abolished, but must make the program have a good readability) Package's named package should be composed of a lowercase word. The name of the Class's named Class must be started by the uppercase of the uppercase letters and other letters. The names of the name variable of the Class variable must be started with a lowercase letter. The latter words begin with uppercase letters. The name of the Static Final variable should be capitalized and point out the full meaning. The name of the naming parameters of the parameters must be consistent with the naming specification of the variable. The named array of arrays should always be named in the following ways: Byte [] buffer; not: byte buffer []; method's parameters use meaningful parameters named, if possible, use the same name as the field to assign : SetCounter (int size) {this.size = size;} Java file style has a Java (*. Java) file must follow the following style rules copyright information copyright information must be on the beginning of the Java file, such as: / ** * Copyright? 2000 Shanghai XXX Co. Ltd. * All Right Reserved. * / Others No need to appear in Javadoc can also be included here. Package / Imports Package Row Before the IMPORT row, the standard package name in the Import is previously in the local package name and arranges in alphabetical order. If the IMPORT line contains different subdirectory in the same package, you should use * to process. 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. Class is next to the annotation of the class, usually used to interpret the class. / ** * a class representing a set of packet and byte counters * it is observable to allow it to be watched, but only * reports changes when the current set is completion * / Next is a class definition, which contains different lines EXTENDS and IMPLEMENTS PUBLIC CLASS CLONEABLE CLASS FIELDS Next is the member variable of the class: / ** * packet counter * / protected int [] packets; member variables for publicoco must generate a document (javadoc). Member variables defined by Proceted, Private and Package If the name meaning is clear, you can not comment. Access method Next is a method of access to class variables. It is just simple to get values to assign values to the class variable, you can simply write on one line.
/ ** * Get the counters * @return an array containing the statistical data This array has been * freshly allocated and can be modified by the caller * / public int [] getPackets () {return copyArray (packets, offset)..; } public int [] getBytes () {return (bytes, offset);} public int [] getpackets () {return packets;} public void setpackets (int [] packets) {this.packets = packets;} other method Don't write on a line of constructors Next to construct a constructor, it should be written in an incremental manner (such as: writing more). Access Type ("Public", "Private", etc.) and any "static", "firm" or "synchronized" should be written in one line, and the methods and parameters can be written separately, which makes the method and parameters easier to read.
Public counterset (int size) {this.size = size;} Clone method If this class can be cloned, then the next step is Clone method: public object clone () {Try {counter = (counterset) Super.clone ); obj.packets = (int []) packets.clone (); obj.size = size; return obj;} catch (CloneNotSupportedException e) {throw new InternalError ( "Unexpected CloneNotSUpportedException:" e.getMessage ()); }}}} 类 方法 方法 方法: / ** * 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 area of equal size //iff (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 }} TSTRING method, no matter what, each class should define the TSTRING method: public string toString () {string retval = "counterset:"; for (int i = 0; i Page width width should be set to 80 characters. Source code will not exceed this width and causing unable to display, but this setting can also be flexible. In any case, the long statement should be in a comma or one action After the folding. After a statement is folded, 2 characters should be retracted more than the original statement. {} The statement in {} should be separate as a line. For example, the first line below is wrong, the second line Is correct: if (i> 0) {i }; // error, {and} in the same row IF (i> 0) {i }; // correct, {Separate as a row} statement forever As a row. If the} statement should be indented to the {that row relatively aligned with it. There should be a space between the parentheses left parentheses and the latter characters. Similarly, there should be no space between the right brackets and the previous characters. The following example shows the incorrect of parentheses and spaces and correct usage: CallProc (APARAMETER); // Error CallProc (APARAMETER); // Do not use meaningless parentheses in the statement. Braces should only be in the source code to achieve some purpose. The following example illustrates errors and correct usage: IF ((i) = 42) {// error - parentheses meaningless if (i == 42) or (j == 42) THEN // correct - correct parentheses Preparation Specification EXIT () EXIT In addition to being called in main, other places should not be called. Because this does not give any code code opportunities to intercept exit. A similar background service ground program should not be exited because a library module determines to exit. An abnormally declared error should throw a runtimeException or derived exception. The top () function of the top layer should intercept all exceptions and print (or record it in the log) on the screen. Garbage collection Java uses mature background garbage collection techniques instead of reference count. But this will result in a problem: You must work after the instance of the object is used. For example, a PREL programmer may write: ... {fileoutputstream fos = new fileoutputstream (projectfile); Project.save (FOS, "IDE Project File");} ... unless the output stream is turned off, Problem language for non-reference counts, such as Java, is a clear work that cannot be automatically completed. Writes below: fileoutputstream fos = new fileoutputstream (projectfile); Project.Save (FOS, "IDE Project File"); fos.close (); Clone is a useful method: imports cloneable public object clone () {TRY {thisclass obj = (thisclass) super.clone (); obj.field1 = (int []) Field1.clone (); obj.field2 = field2; return obj;} catch (clonenotsupportedException E) {throw new interfacerror ( "Unexpected ClonyNotSupportedException:" E.GetMessage ());}} The Final class is definitely not to define the class as final (unless the program framework requirements) because a class is not ready to be inherited. In the documentation, don't define her as final. This is because no one can guarantee that it will not be inherited because of what reason. Most of the members variables of the members of the access class should be defined as protected to prevent inheritance classes. Note, use "int [] packets" instead of "int packets []", the latter is never used. Public void setpackets (int [] packets) {this.packets = packets;} counter} {this.size = size;} Programming Skills BYTE array translation to Characters In order to convert Byte arrays to Characters, you can do this: "Hello World!". GetBytes (); Utility class Utility class (category only) should be declared as abstract to prevent inheritance or initialization. Initializing the following code is a good initialization array method: Objectarguments = new object [] {arguments}; enumeration type Java's support for enumeration, but the following code is a very useful template: Class COLOUR {public static final color black = new color (0, 0, 0); public static final color red = new color final color green = new color final color green = new color (0, 0xFF, 0); Public Static Final Color Blue = New Colour (0, 0, 0xFF); Public Static Final Color White = New Colour (0xFF, 0xFF, 0xFF);} This technology implements RED, Green, Blue, etc. can be like other languages. A constant used as the type of type. They can compare with == operators. But this is used in this way: if a user uses such a method to create a color black new color (0, 0) then this is another object, == operator will generate an error. Her equal () method is still valid. For this reason, the defect of this technology is best to indicate in the document, or only in your own package. Swing avoids the use of AWT components Use the AWT and Swing components If you want to mix the AWT components and Swing components, use it. In fact, try not to mix them up. Scrolling AWT component AWT components must not use JScrollPane classes to implement scrolling. When scrolling the AWT component, you must use the AWT SCROLLPANE component to be implemented. Avoid using AWT components to do so as much as possible in the InternalFrame component, or there will be unpredictable consequences. Z-Order issues The AWT component is always displayed on the Swing component. Be careful when using the POP-UP menu containing the AWT component, try not to use it like this. Debugging debugging is a very important part of software development, there is a part of the software lifecycle. Debug can be opened with configuration, the key is the most basic. A very commonly used debugging method is to use a PrintStream class member, when not defined the trial flow, the class wants to define a DEBUG method to set the stream of debugging. Performance When you write code, you should consider performance issues from head until tails. This is not to say that time should be wasted on the optimization code, but we always remind yourself to pay attention to the efficiency of the code.