Home  >  Article  >  Java  >  Introduction to java naming conventions and naming formats

Introduction to java naming conventions and naming formats

王林
王林forward
2020-11-18 15:17:513105browse

Introduction to java naming conventions and naming formats

Accurate coding formats and specifications are very important to programmers. Let’s share with you the naming specifications and naming formats in Java.

1. Naming specifications

(Learning video sharing: java teaching video)

1. All project names are lowercase

2 , The package name should be all lowercase

3. The first letter of the class name should be capitalized. If the class name consists of multiple words, the first letter of each word must be capitalized.

For example:

public class MyFirstClass{}

4. The first letter of variable names and method names must be lowercase. If the name consists of multiple words, the first letter of each word must be capitalized.

For example:

int index=0;
public void toString(){}

5. Constant names must be in all uppercase letters

For example:

public static final String GAME_COLOR=”RED”;

6. All naming rules must follow the following rules:

  • The name can only be composed of letters, numbers, underscores, and $ symbols

  • It cannot start with a number

  • The name Keywords in JAVA cannot be used.

  • Chinese and Pinyin naming are strictly not allowed.

2. Annotation specifications

1. Class annotation

A class annotation must be added in front of each class. The annotation template is as follows:

/**
Copyright ©, 2006-2010, ChengDu Lovo info. Co., Ltd.
FileName: Test.java
类的详细说明
@author 类创建者姓名
@Date 创建日期
@version 1.00
*/

2. Attribute comments

must be added in front of each attribute. The comment template is as follows:

/** 提示信息 */
private String strMsg = null;

3. Method comments

in each method A method comment must be added in front. The comment template is as follows:

/**
类方法的详细使用说明
@param 参数1 参数1的使用说明
@return 返回结果的说明
@throws 异常类型.错误代码 注明从此类方法中抛出异常的说明
*/

4. Constructor method comment

A comment must be added in front of each construction method. The comment template is as follows:

/**
构造方法的详细使用说明
@param 参数1 参数1的使用说明
@throws 异常类型.错误代码 注明从此类方法中抛出异常的说明
*/

5. Method internal comments

Use single-line or multi-line comments inside the method. The comments are added according to the actual situation.

Such as:

//背景颜色
Color bgColor = Color.RED

Related recommendations: java introductory tutorial

The above is the detailed content of Introduction to java naming conventions and naming formats. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete