Javadoc for local variables?

前端 未结 6 1281
名媛妹妹
名媛妹妹 2021-01-17 09:33

Short question: Is it possible to create Javadoc for local variables? (I just want an explanation for my local variable when hovering over it in Eclipse) Thanks for any hint

相关标签:
6条回答
  • 2021-01-17 10:13

    The local variable should be declared a few lines above its usage. Just use regular comments if you need to. But more importantly, keep methods short, choose meaningful names for them, and declare them only when you need them. Most of the time, it's completely unnecessary to comment local variables.

    Prefer

    int numberOfBooks = books.size();
    

    over

    // the number of books
    int n;
    ... // 50 lines of code
    n = books.size();
    
    0 讨论(0)
  • 2021-01-17 10:20

    It can be done using Annotations.

    Create a simple annotation type such as the following:

    @Retention(RetentionPolicy.SOURCE)
    @Target(ElementType.LOCAL_VARIABLE)
    @interface LocalVariableDocumentation {
        String value();
    }
    

    And use it on your local variable:

    @LocalVariableDocumentation("A very important object!")
    Object anImportantObject;
    

    Eclipse will show the annotation in the tooltip.

    0 讨论(0)
  • 2021-01-17 10:20

    No, it's not supported because JavaDoc generation will ignore it.

    0 讨论(0)
  • 2021-01-17 10:25

    The only way it's possible is with global variables. Local variables cannot be annotated with JavaDoc's.

    0 讨论(0)
  • 2021-01-17 10:33

    Yes it's possible. Just make a regular javadoc comment above the variable.

    public class ExampleClass {
        /** A really cool variable */
        int localVariable;
    
        ...
    

    Now you can hover above the variable in the code further down and the comment will be shown.

    0 讨论(0)
  • 2021-01-17 10:36

    Just make a link to your local variable

    String someLocalVariable;
    /**
     * This a local variable: {@link #someLocalVariable}
     */
    
    0 讨论(0)
提交回复
热议问题