问题
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 :-)
回答1:
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.
回答2:
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();
回答3:
No, it's not supported because JavaDoc generation will ignore it.
回答4:
The only way it's possible is with global variables. Local variables cannot be annotated with JavaDoc's.
回答5:
Just make a link to your local variable
String someLocalVariable;
/**
* This a local variable: {@link #someLocalVariable}
*/
回答6:
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.
来源:https://stackoverflow.com/questions/10566338/javadoc-for-local-variables