1. Introduction
While writing our code, we might refer to articles on the internet like wiki pages, guides, or official documentation of a library. It could be a good idea to add the links to such reference articles in the Javadoc.
In this tutorial, we'll learn how to reference an external URL in Javadoc.
2. Creating an In-Line Link
Java does not offer any special tools for external links, but we can just use standard HTML. The following syntax is used to create an in-line link:
/**
* Some text <a href="URL#value">label</a>
*/
Here, the URL#value can be a relative or absolute URL.
Let's consider an example:
/**
* Refer to <a href="http://www.baeldung.com">Baeldung</a>
*/
This will render as:
Refer to Baeldung
3. Creating an In-line Link With a Heading
Another way is to create a heading containing the link. The @see tag is used as follows to achieve this:
/**
* @see <a href="URL#value">label</a>
*/
Consider the following example:
/**
* @see <a href="http://www.baeldung.com">Baeldung</a>
*/
This will create a ‘See Also' heading containing the link:
See Also:
Baeldung
4. Creating a Link to Javadoc of Another Class
The @link tag is specifically used to link to the Javadoc of other classes and methods. This is an inline tag that converts to an HTML hyperlink pointing to the documentation of the given class or method reference:
{@link <class or method reference>}
Suppose we have a class DemoOne containing a method demo:
/**
* Javadoc
*/
class DemoOne {
/**
* Javadoc
*/
void demo() {
//some code
}
}
Now, we can link to the Javadoc of the above class and method from another class, in the following ways:
/**
* See also {@link org.demo.DemoOne}
*/
/**
* See also {@link org.demo.DemoOne#demo()}
*/
This tag can be used anywhere that a comment can be written, while @see creates its own section.
To summarize, @link is preferred when we use a class or method name in the description. On the other hand, @see is used when a relevant reference is not mentioned in the description or as a replacement for multiple links to the same reference.
5. Conclusion
In this article, we learned about the ways to create an external link in Javadoc. We also looked at the difference between the @see and @link tags.
The post Linking to an External URL in Javadoc first appeared on Baeldung.