Java
Javadoc
see
link
Documentation

Javadoc see or link?

Master System Design with Codemia

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

In Java documentation, the @see and {@link} tags are pivotal in creating comprehensive and navigable API documentation. Both are utilized within Javadoc comments and serve to provide references to other elements within the code or even external documentation. However, they have distinct differences in their usage and behavior.

Understanding @see and {@link}

@see

The @see tag is used to create a "See Also" reference at the end of a Javadoc comment. This reference points to a related class, method, or field. Typically, the @see tag is used to provide a general reference rather than inline text.

Syntax

java
@see java.util.List

The tag takes the fully qualified name of a class, method, or field. When used, it generates a hyperlink in the generated HTML documentation, which directs the reader to the specified reference.

Example

Suppose we have the following class with a method reference in a Javadoc comment:

java
1/**
2 * This class demonstrates the use of the @see tag.
3 *
4 * @see java.util.ArrayList
5 */
6public class ListExample {
7    // Class code...
8}

This creates a link in the generated Javadoc pointing to the ArrayList class in the standard Java library, suggesting to the reader that ArrayList is relevant to the current class discussion.

{@link}

In contrast, the {@link} tag is used to create an inline link within a Javadoc comment. It allows you to hyperlink a word or phrase within the main body of text itself, making it more fluid for the reader.

Syntax

java
{@link java.util.List}

This tag also takes the fully qualified name of a class, method, or field. However, it allows the reader to click on the embedded reference in the documentation where it appears directly within a paragraph or list.

Example

Consider the following usage within a method comment:

java
1/**
2 * This method uses {@link java.util.HashMap} to store data.
3 */
4public void storeData() {
5    // Method code...
6}

In this example, "HashMap" within the comment body will be an interactive link pointing to its API documentation.

Key Differences

Here's a summarized comparison between @see and {@link}:

Aspect@see{@link}
PurposeGenerates a "See Also" listInline text hyperlink
UsageStand-alone, post-descriptionWithin descriptive text
AppearanceAt the end of the Javadoc comment blockInline with other text
Linking styleList of references at the end of the Javadoc commentEmbedded within sentences
Use caseBroad referencesContextual, specific references

Additional Considerations

Alternative Tag: {@linkplain}

  • Purpose: Similar to {@link} but generates plain-text links.
  • Use Case: When you want a link in the Javadoc without the default link style (useful for when style formatting needs customization).

For cases when you want to refer to an external source or a non-public member, you may use HTML anchor tags directly within Javadocs:

java
1/**
2 * For more information, see <a href="https://docs.oracle.com">Oracle Documentation</a>.
3 */
4public class ExternalLinkExample {
5    // Class code...
6}

Best Practices

  • Choose Appropriately: Use @see when you need to suggest additional reading or related classes. Use &#123;@link&#125; to create more engaging and well-connected documentation.
  • Syntax Precision: Ensure that names are fully qualified if they are not within the same package or if ambiguity might arise.
  • Documentation Quality: Always ensure that the linked references enhance understanding rather than just being presentational decorations.

These tags are essential tools in a developer's toolkit for creating robust and navigable documentation, substantially aiding in the maintenance and usability of the code for others accessing it. By understanding when and how to use each, developers can create clearer, more informative Javadocs that contribute to better code literacy and usability.


Course illustration
Course illustration

All Rights Reserved.