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 :-)
-
1You can include variable description into method's JavaDoc comment – Aleksandr Kravets May 16 '12 at 06:31
-
1Sometime when you have to debug an other person code and local variables are messy, it would help. – Ced Nov 20 '17 at 16:06
6 Answers
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.

- 5,313
- 3
- 35
- 73
-
What import is required for this? There is little to no documentation from search engines. – Richard Jessop Jan 20 '22 at 13:12
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();

- 678,734
- 91
- 1,224
- 1,255
-
11I agree it's unnecessary *most of the time*, but there are just things that are more complex to explain than number of books and such stuff, especially when doing more complex calculations etc. – stefan.at.kotlin May 12 '12 at 18:46
The only way it's possible is with global variables. Local variables cannot be annotated with JavaDoc's.

- 21
- 1
Just make a link to your local variable
String someLocalVariable;
/**
* This a local variable: {@link #someLocalVariable}
*/

- 21
- 4
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.

- 327
- 1
- 4
-
I too thought this was the case. Maybe it was an older version of Eclipse. – jonS90 Sep 06 '17 at 19:35