knowledge-kitchen / course-notes

Documenting Source Code With Javadoc

All programs must include javadoc comments in the source code.

Overview

Javadoc is a tool built into the Java Development Kit that automatically generates documentation of your source code based on comments written in a particular syntax, and allows development environments, such as Eclipse, to offer intelligent tooltips that show this documentation when using methods or classes commented in such a manner. The javadoc tool depends upon you adding special comments into your code that include the necessary documentation. In order to use it effectively, you must add javadoc comments for all class definitions, method definitions.

The javadoc tool is run from Eclipse by clicking Project->Generate javadoc. This generates documentation in a folder named doc within your project folder. You can open this documentation in your web browser.

Class definitions

Required documentation

Example

/**
  * A program to output the message, "Hello world".
  *
  * @author Foo Barstein
  * @version 0.1
  */
  public class HelloWorld {
    // ...
  }

Method definitions

Required documentation

Example

/**
  * Calculates the sum of two numbers.
  *
  * @param num1 The first number, as an integer.
  * @param num2 The second number to add to the first
  * @return The sum of the two integers
  * @exception IllegalArgumentException if num1 or num2 is less than 0
  */
  public static void sum(int num1, int num2) throws IllegalArgumentException {
    // ...
  }

More

The documentation outlined here is the bare minimum of what javadoc offers. Here is a more complete reference list adapted from David Flanagan, Java in a Nutshell, Second Edition, (O’Reilly 1997), including javadoc tags that are not required in this course:

@author text

@param parameter-name description

@return description

@exception full-classname description

@see classname

@see full-classname Type, Method, Field

@see full-classname#method-name Type, Method, Field