Java
Javadoc
Programming
Method Referencing
Documentation

How to reference a method in javadoc?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

When writing Java code, documenting methods using Javadoc is crucial for providing clear and useful information to other developers, maintaining code effectively, and enhancing the programming experience with your API. Javadoc comments are written in a special format that is then converted into HTML documentation by the Javadoc tool. Properly referencing methods in Javadoc is essential for creating comprehensive and navigable documentation.

Javadoc Basics

Javadoc comments are block comments starting with /** and ending with */ placed above classes, interfaces, constructors, methods, and fields. They can include a description, tags and special annotations to provide additional information or to link to other parts of the documentation.

Referencing Methods

To reference a method within Javadoc, you use the {@link} tag. This provides a clickable link to the specified method when the Javadoc is generated.

Syntax and Examples

Here is the general syntax to reference a method in Javadoc:

java
{@link #methodName(parameterType1, parameterType2)}

For example, consider a Java class named MathOperations with a method add:

java
1public class MathOperations {
2    /**
3     * Adds two integers.
4     * For subtraction, see {@link #subtract(int, int)}.
5     *
6     * @param a first number
7     * @param b second number
8     * @return the sum of a and b
9     */
10    public int add(int a, int b) {
11        return a + b;
12    }
13
14    /**
15     * Subtracts two integers.
16     * @param a first number
17     * @param b second number
18     * @return the result of a minus b
19     */
20    public int subtract(int a, int b) {
21        return a - b;
22    }
23}

In the add method documentation, {@link #subtract(int, int)} creates a link to the subtract method.

Detailed References

  • Reference Constructors: If you need to reference a constructor, use the class name followed by the parameters.
  • No-argument Methods: If the method takes no arguments, simply skip the parentheses in the reference.
  • Overloaded Methods: Specify the parameter types to link to the correct method, especially necessary when methods are overloaded.

Additional Tags for Referencing

Other than {@link}, there are additional tags:

  • {@linkplain}: This works like {@link} but renders the link in plain text, not code font.
  • {@see}: Used to indicate a "See Also" section typically placed at the end of the documentation comment.

Example of {@see}

java
1/**
2 * Multiplies two integers.
3 * @param a first number
4 * @param b second number
5 * @return product of a and b
6 * @see #add(int, int)
7 */
8public int multiply(int a, int b) {
9    return a * b;
10}

Summary Table

TagDescriptionExample Usage
{@link}Inserts inline link{@link #methodName(type)}
{@linkplain}Inserts plain text link{@linkplain #methodName()}
{@see}Adds "See Also" section reference@see #methodName(type1, type2)

Best Practices

  1. Use Full Signatures: Providing parameter types in method links helps avoid ambiguity, especially in classes with overloaded methods.
  2. Consistency: Be consistent in how you reference methods in your project documentation.
  3. Testing: Generate and review the Javadoc to ensure that all links work correctly and that the documentation is clear.
  4. Clarification: Use the link text to clarify the context if necessary, e.g., {@link ClassName#methodName() ClassName's methodName}.

Properly referencing methods in Javadoc not only improves documentation but also enhances the ease of navigation and comprehension, making it a vital practice for effective Java documentation.


Course illustration
Course illustration

All Rights Reserved.