search
HomeJavajavaTutorialJava Coding Standards (Commonly Used Key Points)

Java Coding Standards (Commonly Used Key Points)

May 10, 2018 am 10:15 AM
javaCommonly 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;

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

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

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

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

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

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

@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
 */

方法注释

使用下面的模版

/**
 * ${todo}
 * ${tags}
 */

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

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

如果类中的方法实现了抽象方法或重写了父类的方法,应在方法上加上@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;

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

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");
}

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

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

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

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;
}

以上是我所整理的一部分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
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
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

See all articles

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)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.