Java Naming rules

Source: Internet
Author: User

Java Naming rules

1. File

1. The property file is suffixed with properties and complies with the i18n specification in java;
2. The configuration files of each product module must be placed in the conf directory of the module;
3. the file name must be as short as possible. The name must meet the requirements of the operating system and must be in English or Chinese pinyin format. The file name cannot contain spaces, use a slide line between common file words and words.

2. Naming rules

2.1 Basic Rules

1. the character set contains 26 English letters, 0 to 9 Arabic numerals and underscores. Classes, fields, methods, variables, and constants in Java should be expressed in letters as much as possible.
2. The name must be meaningful. We recommend that you use the terminology in the problem domain to make the name self-described to a certain extent.
3. The name should be as short as possible. If the name is too long, you can use the alias or abbreviation to simplify the name. Abbreviations must be meaningful and must be maintained throughout the project.
4. Naming rules (applicable to Class Name, field name, variable name, Module name, etc)
1) delete all original audio letters and compress duplicate letters. Such as button, abbreviated as btn,
2) In case of a name conflict, the original sound is retained in a certain abbreviation. For example, in order not to conflict with the button, batn is abbreviated
5. Do not use leading underscores (_) or underscores (_) at the end of the name.

 2.2 constant naming rules

1. All characters must be capitalized. A meaningful word combination is used for expression. Words and words are separated by underscores.
2. The name should be as short as possible and should not exceed 16 characters

In program development, it is best not to directly work on literal. It is best to introduce the constant method for application. It can only be used in special cases. for example, you can use-When initializing a variable in a for loop, 1. These constants

Example

public final int MAX_SIZE = 120;public final int MAX_WIDTH = 100;public final String PROPERTY_NAME= "menu" ;

2.3 variable naming rules

1. Avoid using numbers in naming. Unless the naming conventions are clear and the program is clearer, there should be no numbers in naming instance variables.
2. variable name is a noun meaning
3. Use a combination of words or words that match the meaning of the problem domain. The first word is all in lower case, and each subsequent word is in upper case with the first letter and the other in lower case (except for special words, such as URLs)
4. The name should be as short as possible and should not exceed 16 characters
5. In addition to temporary variables with short lifecycles, do not use a single character as the variable name. Do not use a single character for instance variable naming. Common Single-character variables such as integer type I, j, k, m, n using c, d, e, coordinate using x, y, z.
6. in some cases, a variable may need to be prefixed with a type. The prefix of all types must be in lower case. There is no interval between the variable and the object part of the variable name. Each word in the Entity part is in upper case, the rest of the characters are in lowercase (except for special words such as URLs). We strongly recommend that you use global variables for the class. The general type prefix is as follows:

Type prefix

Type

Example

B

Boolean and boolean

Bsingle

F

Floating Point Number

Fsize

D

UFDouble

Dmoney

Dt

Date

DtToday

C

Character, char

Cinput

Obj

OBJECT variable

ObjUser

Str

String (String, StringBuffer)

StrFileName

I

Integer

ICount

Ary

Array

AryName

Conn

Connection

ConnActiveConnection

Stmt

Statement

StmtFindUser

Rs

Resultset

RsUsers

ArrayList

Al

AlData

Map

Map

Key_value_Map

Set

Set

ValueSet

Col

Collection

ColNames

Msg

Message

MsgText

Err

Error

ErrCode

Btn

Button

BtnSubmit

 

 

 










































Example
public int width;public String fileName;public static ApplicationContext context;

2.4 naming rules for methods

1. Use a combination of words or words that match the meaning of the problem domain. The first word is in lower case, the subsequent words are in upper case, and the rest are in lower case (except for special words such as URLs). There is no special reason not to use underscores as separators.
2. The name of the attribute method in Java follows the JavaBean standard:
1) getter method: get + attribute name. It is better to use the is + attribute name for the boolean type, and use has and can instead of is for some specific attribute names.
2) setter method: set + attribute name
3. the constructor name is the same as the class name.

2.5 class and interface naming rules

1. Use a combination of words or words that match the meaning of the problem domain. The first letter of each word is capitalized, And the other letters are lowercase (except for special words such as URLs)
2. the first character of the interface is

Example

Public class Fiugrepublic interface FiugreContainerpublic class StdFigure // std stands for Standard

Naming rules for 2.6 packages

1. All characters in the package name are in lowercase;
2. Do not have two-way dependency between two packages of different services. You can use one-way dependency;
3. Adopt a logical hierarchy to reduce dependencies

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.