codestyle; put javadoc before or after annotation?

2019-01-16 13:44发布

I know that it isn't the most vital of issues, but I just realised that I can put the javadoc comment block before or after the annotation. What would we want to adopt as a coding standard?

/**
 * This is a javadoc comment before the annotation 
 */
@Component
public class MyClass {

    @Autowired
    /**
     * This is a javadoc comment after the annotation
     */
    private MyOtherClass other;
}

4条回答
不美不萌又怎样
2楼-- · 2019-01-16 13:55

Before the annotation, since the annotation is code that "belongs" to the class. See examples with javadoc in the official documentation.

Here's random example I found in another official Java page:

/**
 * Delete multiple items from the list.
 *
 * @deprecated  Not for public use.
 *    This method is expected to be retained only as a package
 *    private method.  Replaced by
 *    {@link #remove(int)} and {@link #removeAll()}
 */
@Deprecated public synchronized void delItems(int start, int end) {
    ...
}
查看更多
该账号已被封号
3楼-- · 2019-01-16 13:57

I agree with the answers already given.

Annotations are part of the code while javadoc is part of the documentation (hence the name).

So for me it seams reasonable to keep the code parts together.

查看更多
ゆ 、 Hurt°
4楼-- · 2019-01-16 14:00

It all comes down to readablity. In my opinion code is more readable with the Annotations directly above the method/field.

查看更多
相关推荐>>
5楼-- · 2019-01-16 14:04

Apart of the coding standard, it seems that javadoc tool doesn't process the java doc comments if they are placed after the annotations. Works fine otherwise.

查看更多
登录 后发表回答