408

Is there a way to add references to one or more of a method's parameters from the method documentation body? Something like:

/**
 * When {@paramref a} is null, we rely on b for the discombobulation.
 *
 * @param a this is one of the parameters
 * @param b another param
 */
void foo(String a, int b)
{...}
Robin Rex
2303 silver badges10 bronze badges
asked Nov 3, 2009 at 13:00
0

5 Answers 5

446

As far as I can tell after reading the docs for javadoc there is no such feature.

Don't use <code>foo</code> as recommended in other answers; you can use {@code foo}. This is especially good to know when you refer to a generic type such as {@code Iterator<String>} -- sure looks nicer than <code>Iterator&lt;String&gt;</code>, doesn't it!

Jacek Laskowski
75k28 gold badges253 silver badges440 bronze badges
answered Nov 4, 2009 at 0:50
Sign up to request clarification or add additional context in comments.

2 Comments

How is it not possible to reference a method's own parameters within the docs of the method. Swing and a miss, Java!
105

The correct way of referring to a method parameter is like this:

enter image description here

answered Jan 13, 2017 at 11:34

9 Comments

Not only does it answer the question, but it visually explains how to amend Javadoc with a parameter using an IDE such as Intellij. This will be useful for searchers who are looking for an answer.
On Eclipse it doesn't work. But it's a nice answer nonetheless
@user4504267 Image looks fine, at least now.
Try to refactor param name, intellij won't update this code blocks.
Despite the pretty picture, ignore this answer and see the accepted answer from @kevin-bourrillion because it says to do the same thing but notes that it is only a workaround as no such feature exists (... and so you should not expect an actual link or for refactoring to update it etc.).
|
69

As you can see in the Java Source of the java.lang.String class:

/**
 * Allocates a new <code>String</code> that contains characters from
 * a subarray of the character array argument. The <code>offset</code>
 * argument is the index of the first character of the subarray and
 * the <code>count</code> argument specifies the length of the
 * subarray. The contents of the subarray are copied; subsequent
 * modification of the character array does not affect the newly
 * created string.
 *
 * @param value array that is the source of characters.
 * @param offset the initial offset.
 * @param count the length.
 * @exception IndexOutOfBoundsException if the <code>offset</code>
 * and <code>count</code> arguments index characters outside
 * the bounds of the <code>value</code> array.
 */
public String(char value[], int offset, int count) {
 if (offset < 0) {
 throw new StringIndexOutOfBoundsException(offset);
 }
 if (count < 0) {
 throw new StringIndexOutOfBoundsException(count);
 }
 // Note: offset or count might be near -1>>>1.
 if (offset > value.length - count) {
 throw new StringIndexOutOfBoundsException(offset + count);
 }
 this.value = new char[count];
 this.count = count;
 System.arraycopy(value, offset, this.value, 0, count);
}

Parameter references are surrounded by <code></code> tags, which means that the Javadoc syntax does not provide any way to do such a thing. (I think String.class is a good example of javadoc usage).

Koray Tugay
24k49 gold badges206 silver badges335 bronze badges
answered Nov 3, 2009 at 13:40

2 Comments

The <code></code> tag is not referencing a specific parameter. It is formatting the word "String" into "code looking" text.
@Naxos84 that's true for the first <code></code> tag, but further on in the javadoc they reference offset and count parameters surrounded by <code></code> tags
14

I guess you could write your own doclet or taglet to support this behaviour.

Taglet Overview

Doclet Overview

answered Nov 3, 2009 at 13:25

1 Comment

And make a pull request to javadoc :)
-2

Here is how it is written in Eclipse Temurin JDK 8 sources:

enter image description here

It looks like the only way is or {@code }, but it's not a link - it's just formatting.

answered Feb 3, 2022 at 12:02

2 Comments

So.. other than under Params: section, where is x located in the javadoc description of println ?
This is an incorrect answer! {@link #method(parameters)} works only for a link to other methods of the same class, not to the parameters of the same method, which was the question.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.