26. Documentation with Javadoc

Documentation is a crucial aspect of software development. It allows other developers to understand how to use and contribute to a project, and makes the code easier to maintain over the long term. In the Java world, Javadoc is the standard tool for generating source code documentation in HTML format, using special comments within the code. Next, we'll explore how to use Javadoc to effectively document a Java project.

What is Javadoc?

Javadoc is a tool provided by the JDK (Java Development Kit) that extracts documentation comments from Java source code and generates HTML documentation pages. Documentation comments are written in a specific format, starting with /** and ending with */, and are placed just above declarations of classes, interfaces, constructors, methods and variables.

Writing Javadoc Comments

To get the most out of Javadoc, it is important to write clear and informative comments. Here are some common elements used in Javadoc comments:

  • Description Tags: They are used to describe the purpose of a class, method or field. For example, @param describes a method parameter, @return describes the return value of a method, and @throws (or @exception) describes the exceptions that a method can throw.
  • Link: The {@link} tag allows you to create links to other parts of the documentation.
  • Since: The @since tag tells you in which version of the software a feature was introduced.
  • See Also: The @see tag provides references to other items listed in the documentation.
  • Deprecated: The @deprecated tag indicates that a method or class should no longer be used and often suggests an alternative.

A basic example of a Javadoc comment for a method would be:

/**
 * Calculates the sum of two integers.
 *
 * @param the first integer to be added
 * @param b the second integer to be added
 * @return the sum of a and b
 */
public int sum(int a, int b) {
    return a + b;
}

Generating Documentation

To generate HTML documentation, you can use the javadoc command-line tool that comes with the JDK. For example, to document a class called Example.java, you would use the command:

javadoc Example.java

This will generate a set of HTML files describing the class and its members, which you can open in any web browser.

Customizing Documentation

The Javadoc allows for a series of customizations through command-line options and additional tags. For example, you can use the -author option to include author information in the documentation or -version to include the version of the class or interface.

Also, you can use the @docRoot tag to reference the HTML documentation root path, which is useful for creating relative links.

Good Practices

When documenting your code with Javadoc, here are some best practices to follow:

  • Document all public classes and interfaces, as well as their public methods and fields.
  • Be clear and concise in your descriptions. Avoid unnecessary information.
  • Use code examples when they help clarify how to use a method or class.
  • Keep documentation updated as code changes.

Integration with IDEs and Build Tools

The main IDEs (Integrated Development Environments), such as Eclipse, IntelliJ IDEA and NetBeans, have integrated support for Javadoc, allowing you to generate and view documentation directly from the development environment. Additionally, build tools like Maven and Gradle have dedicated plugins or tasks to generate Javadoc documentation as part of the build process.

Conclusion

Using Javadoc to document Java code is an essential practice to ensure that your code is understandable and maintainable. By following Javadoc conventions and best practices, you can create rich, useful documentation that benefits both current and future developers working on your project. Remember that well-written documentation is just as important as the code itself, as it makes it easier to collaborate, maintain, and effectively use the software you create.

Now answer the exercise about the content:

Which of the following statements about the use of Javadoc is correct?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Continuous integration and build tools like Maven/Gradle

Next page of the Free Ebook:

122Continuous integration and build tools like Maven/Gradle

4 minutes

Obtenez votre certificat pour ce cours gratuitement ! en téléchargeant lapplication Cursa et en lisant lebook qui sy trouve. Disponible sur Google Play ou App Store !

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text