Home Java javaTutorial Java Coding Standards (Commonly Used Key Points)

Java Coding Standards (Commonly Used Key Points)

May 10, 2018 am 10:15 AM
java Commonly used

The following is the Java coding specification I compiled. When writing code, you must follow the coding specification to facilitate daily maintenance.

Naming convention

##Class naming convention

The first letter of each word in the class needs to be capitalized, such as UserService, wrong naming method userService, userservice

The test case ends with Test, such as UserServiceTest

If it is abbreviated with terms At the beginning, term abbreviations should be all capitalized, such as HTMLEditor. Wrong writing...

Class names should use English letters or numbers, and no special characters should appear

Interfaces do not start with I

Method naming convention

The first letter of the first word is lowercase, and the first letter of other words is capitalized

It should be visible from the method name The role of the method

Coding specifications

##Code indentationCode indentation is a tab (length of 4 spaces). Eclipse defaults to a length of 4 spaces.

ScopeThe properties in the class should be set to private, and external classes can modify private properties by providing get and set methods.

If the method in the class is only for internal use of the class, it should be set to private; if it can be used by subclasses, it should be set to protected; if it is a public method, it should be set to public.

Comment specification

Copyright information commentCopyright information comment At the beginning of the file, used to declare the copyright of the code. Use comments like /**/.

/* 
 * Copyright ©  2015 TIAMAES Inc. All rights reserved.
 */
package com.tiamaes.gjds.das.controller;
Copy after login

The comment template is as follows, Window->Preferences->Java->Code Style->Comments->Files

/*
* Copyright ©  ${year} TIAMAES Inc. All rights reserved.
*/
Copy after login

Copyright © 2015 TIAMAES Inc. All rights reserved. The description is as follows

Copyright 2015 Tianmai Technology Co., Ltd. All rights reserved.

- Inc. A joint stock limited company formed in accordance with the Company Law
- Co. Ltd Limited liability company

Class annotation specifications

Class annotation information should be Contains class description information, author information and version information.

/**  
 * 类描述
 * @author 王成委
 * @since 1.0
 * @see xxx
 */
public class TypeName
Copy after login

The comment template is as follows, Window->Preferences->Java->Code Style->Comments->Types

/**  
 * ${todo}
 * @author 王成委
 * @since 1.0
 */
Copy after login

类描述:描述类的功能,单行直接写,如果多行需要使用

@author:多个作者使用多个@author

@since:说明此类是从那个版本开始

@see:与类相关的其他类或方法
可参考org.springframework.stereotype.Controller的类注释信息。

/**
 * Indicates that an annotated class is a "Controller" (e.g. a web controller).
 *
 * <p>This annotation serves as a specialization of {@link Component @Component},
 * allowing for implementation classes to be autodetected through classpath scanning.
 * It is typically used in combination with annotated handler methods based on the
 * {@link org.springframework.web.bind.annotation.RequestMapping} annotation.
 *
 * @author Arjen Poutsma
 * @author Juergen Hoeller
 * @since 2.5
 * @see Component
 * @see org.springframework.web.bind.annotation.RequestMapping
 * @see org.springframework.context.annotation.ClassPathBeanDefinitionScanner
 */
Copy after login

方法注释

使用下面的模版

/**
 * ${todo}
 * ${tags}
 */
Copy after login

${tags}:自动生成参数、异常、返回值等注解

/**
 * TODO
 * @param request
 * @throws IOException
 */
@RequestMapping("/ctx")public void test(HttpServletRequest request) throws IOException
Copy after login

如果类中的方法实现了抽象方法或重写了父类的方法,应在方法上加上@Override注解。如果要覆盖父类方法的注释可以使用/** */注释来覆盖父类的注释。

org.springframework.core.io.Resource
/**
 * Return a File handle for this resource.
 * @throws IOException if the resource cannot be resolved as absolute
 * file path, i.e. if the resource is not available in a file system
 */
File getFile() throws IOException;
Copy after login

在实现的方法上使用/**...*/可以覆盖父类方法的注释。

org.springframework.core.io.AbstractResource
/**
 * This implementation throws a FileNotFoundException, assuming
 * that the resource cannot be resolved to an absolute file path.
 */
@Overridepublic File getFile() throws IOException {    
throw new FileNotFoundException(getDescription() + " cannot be resolved to absolute file path");
}
Copy after login

属性和变量及方法内代码的注释

使用//来对变量和属性注释,方法内的代码也使用//注释

如非必要变量和属性可以不加注释,如果代码内有负责逻辑应使用//注释加以说明

public ServletContextResource(ServletContext servletContext, String path) {    // check ServletContext
    Assert.notNull(servletContext, "Cannot resolve ServletContextResource without ServletContext");    this.servletContext = servletContext;    // check path
    Assert.notNull(path, "Path is required");
    String pathToUse = StringUtils.cleanPath(path);    
    if (!pathToUse.startsWith("/")) 
    {
      pathToUse = "/" + pathToUse;
    }    
    this.path = pathToUse;
}
Copy after login

以上是我所整理的一部分JAVA编码规范,希望今后大家做开发时能够严格按照编码规则来开发,这样有便于日常维护。


The above is the detailed content of Java Coding Standards (Commonly Used Key Points). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

15 commonly used currency circle escape index technology analysis 15 commonly used currency circle escape index technology analysis Mar 03, 2025 pm 05:48 PM

In-depth analysis of the top 15 Bitcoin Escape Index: Market Outlook for 2025 This article deeply analyzes fifteen commonly used Bitcoin Escape Index, among which the Bitcoin Rhodl ratio, USDT current wealth management and altcoin seasonal index have reached the Escape Index in 2024, attracting market attention. How should investors deal with potential risks? Let us interpret these indicators one by one and explore reasonable response strategies. 1. Detailed explanation of key indicators AHR999 coin hoarding indicator: Created by ahr999, assisting Bitcoin fixed investment strategy. The current value is 1.21, which is in the wait-and-see range, so it is recommended to be cautious. Link to AHR999 Escape Top Indicator: A supplement to AHR999 Coin Hoarding Indicator, used to identify the top of the market. The current value is 2.48, this week

See all articles