Java coding guide (1)

Source: Internet
Author: User
1. Naming rules1.1 package naming rules

[Java-1]The package name should be written in small form and separated by "." In the middle [required]

Description · reason

Unless otherwise specified, make sure that the package name is written in small form. This is a general Java rule.

Example

Package CN. co. AAA. BBB; // Error

Package CN. co. AAA. BBB; // correct

 

[Java-2]The package name must be meaningful [required]

Description · reason

Try to name a package that can be associated with the package content. If you use the function ID and serial number to name a package, you cannot understand it immediately. Compliance with this specification will improve the ease of coding.

Example

Package CN. co. AAA. BBB. s00001; // Error

Package CN. co. AAA. BBB. Station; // correct

 

[Java-3]The package name cannot be omitted. [required]

Description · reason

Even if the package name is long, do not omit it.

Example

Package CN. co. AAA. BBB. T. E. S. t; // Error

Package CN. co. AAA. BBB. Test; // correct

 

[Java-4]The package name cannot contain any character other than lowercase English letters, but the underscore "_" can be used as needed. [required]

Example

Package CN. co. AAA. BBB.My_app;

 

1.2 import naming rules

[Java-5]The order of import packages is Java. * → javax. * → package provided by middleware, library, etc. → package in the program [provision]

Example

Import java. util. hashmap;

Import java. util. Map;

 

Import javax. servlet. http. httpservletrequest;

Import javax. servlet. http. httpservletresponse;

 

Import org. Apache. log4j. appender;

Import org. Apache. log4j. category;

 

Import CN. co. AAA. BBB. thatpack;

 

[Java-6]Import specifies which class to import and prohibits the import of the entire package. [recommended]

Example

Import java. util. *; // Error

Import java. util. hashmap; // correct

 

1.3 class naming rules

[Java-7]The class name should be a noun phrase. It can be an adjective, noun, or noun. [recommended]

Example

Public class inputdatastream {

Public class orderitem {

Public class developer {

 

[Java-8]Class Name should reflect its function [recommended]

Description · reason

Do not use the function ID, serial number, or other obscure names in the class name. The class name should use a meaningful string so that you can think of the content of its class. This will improve the ease of coding.

Example

Public class s0001 {// Error

Public class sample {// correct

 

[Java-9]The first letter of a word in the class name should be capitalized [required]

Description · reason

Please use up to 1st letters of the class name. When the class name is composed of multiple words, the first letter (paragraph) of each word must be capitalized. This is a general Java rule.

Public class sampleclass {// Error

Public class sampleclass {// correct

 

[Java-10]Add the exception class name at the end"Exception"[Recommended]

Description · reason

For the exception class name, add "exception" at the end of the name ". This statement improves the ease of coding.

Example

Public class sample extends exception {// Error

Public class sampleexception extends exception {// correct

 

[Java-11]The interface name must be based on the class name [recommended]

Description · reason

The interface naming rules are basically based on the class name. This statement improves the ease of coding. To distinguish it from the class name, add "I" at the beginning ".

Example

Public class sample implements isample {// Interface Name isample

 

[Java-12]Abstract class names should be based on class names [recommended]

Description · reason

The naming rules for abstract class names are basically based on class names. This statement improves the ease of coding. The name must start with "abstract" and be associated with the name of subclass.

Example

Public abstract class abstractsample {// The name of the abstract class is abstractsample.

 

[Java-13]Implementation of class names should be based on class names [recommended]

Description · reason

The naming rules for implementing class names are basically based on class names. This statement improves the ease of coding. If you need to distinguish it from the interface, add "impl" at the end ".

Example

Public class sampleimpl implements isample {// implementation class name sampleimpl

 

[Java-14]Add"Able"[Recommended]

Description · reason

For the class, there will be interfaces with certain capabilities, such as runnable and cloneable. When defining an interface with additional capabilities, use the adjective (~ Can. This statement improves the ease of coding.

Example

Public class sample implements ipluggable {

 

[Java-15]The format of the test class name is "test object class name+ Test"[Recommended]

Description · reason

The format of the test class name is "test object class name + test ". This statement improves the ease of coding. The following is an example of JUnit.

Example

Public class sampleclasstest extends testcase {

 

[Java-16]The format of all class names for testing or package testing is"Alltest"Or" package name+ Test"[Recommended]

Description · reason

All class names for testing or package testing are in the format of "alltest" or "package name + test ". This statement improves the ease of coding.

Example

Public class alltest {

 

Package CN. co. AAA. BBB;

Public class stationtest {

 

1.4 method naming rules

[Java-17]The method name is only used for writing a paragraph. [required]

Description · reason

When there is only one word for the method name, all the words are written in small form. When it is composed of multiple words, the first 2nd letters of the 1st words after the first are capitalized. This is a general Java rule.

Example

Public void samplemethod () {}// Error

Public void samplemethod () {}// correct

 

[Java-18]The format of the method name for generating the object is""Create" +Object Name "[recommended]

Description · reason

For the method (Factory method) Name of the generated object, start with "CREATE" and add the name of the object generated by this method after it. This statement improves the ease of coding.

Example

Public sample createsample (){

 

[Java-19]The format of the conversion method name is""To" +Object Name "[recommended]

Description · reason

The method (converter method) for converting an object to another object starts with "to" and adds the name of the converted object to it. This statement improves the ease of coding.

Example

Public another toanother (){

 

[Java-20]The format of the getter method name is""Get" +Attribute name "[recommended]

Description · reason

For the name of the method (getter method) for obtaining an attribute, start with "get" and add the obtained attribute name after it. This statement improves the ease of coding. This is the specification of JavaBeans.

Example

Public String getsamplename (){

 

[Java-21]The format of setter method name is""Set" +Attribute name "[recommended]

Description · reason

For the method (setter method) Name of the property, start with "set" and add the property name after it. This statement improves the ease of coding. This is the specification of JavaBeans.

Example

Public void setsamplename (string name ){

 

[Java-22]The returned value isBooleanThe name of the method type should be visible.True/FalseStatus [recommended]

Description · reason

For method names of Boolean variables returned, you must be able to see the status of true/false returned values. We recommend that you use a description (for example, is + business card) that represents a yes or no question ). This statement improves the ease of coding.

Example

Public Boolean isasleep (){

Public Boolean canspeak (){

Public Boolean hasexpired (){

Public Boolean exists (){

Public Boolean hasvalue (){

 

[Java-23]Use Find as the prefix in the query method [recommended]

Description · reason

Example

public String findNameByID (String ID){

 

1.5 variable naming rules

[Java-24]All constants arePublic static final, All are big writing, paragraph with"_"[Required]

Description · reason

All constants are declared using static final, and all variable names are described using uppercase letters. When a constant name is composed of multiple words, each word is separated. This statement improves the ease of coding. In addition, changing the public modifier of constants to protected and private is of no particular significance.

Example

Public static final int sample_value = 10;

 

[Java-25]Variable names must reflect their functions [required]

Description · reason

Variable names must reflect their functions. This statement improves the ease of coding.

Example

Private string str1;

Private string str2; // Error

Private string servername;

Private string clientname; // correct

 

[Java-26]The format of the instance variable name is "'_'+Variable name "[recommended]

Description · reason

For instance variable names, use the parameter names of "_" and method or the local variable names. This will improve the ease of coding.

Example

Private string name;

Public void setname (string name ){

This. Name = Name;

} // Error

Private string _ name;

Public void setname (string name ){

This. _ name = Name;

} // Correct

 

[Java-27]Use uppercase letters for all field names of an instance [Rules]

Description · reason

The field names of instances must all be in big format. The instance fields are often static and final. Therefore, you need to name these fields according to the same naming rules as the constant name.

Example

interface IFV {
Int max = 1000; // Error
}
interface IFV {
Int max = 1000; // correct
}

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.