javadoc 如何知道每个文档注释是针对哪个方法的?

How does the javadoc know which method each documentation comment is for?

在下面的代码中:

public class Rectangle {
    private double length;
    private double width;

    /**
     * The setLength method stores a value in the
     * length field.
     * @param len The value to store in length.
     * 
     */

    public void setLength(double len){
        length  = len;
    }

javadoc 如何将我的评论与 setLength 方法关联起来?仅仅是因为注释出现在方法声明之前吗?

是的,确实很简单,因为注释正好在方法声明之前。

虽然据我所知没有明确说明,但它来自对 JavaDoc 工具工作原理的描述,请参阅 How to Write Doc Comments for the Javadoc Tool

具体来说,关于 Source Files 的部分:

The Javadoc tool can generate output originating from four different types of "source" files:

  • Source code files for Java classes (.java) - these contain class, interface, field, constructor and method comments.
  • [...]

A doc comment is written in HTML and must precede a class, field, constructor or method declaration.