Guidelines for writing
Javadoc documentation comments

Maintained by Reinhold Plösch and Rainer Weinreich

Version 1.1P
8.3.2000

This document describes the formatting and content conventions we use in documentation comments for Java programs. It concentrates on conventions that should be adhered to. The document is an adapted version of Sun's "How to Write Doc Comments for Javadoc" document.

Contents

Types of Source Files

Javadoc can generate output originating from four different types of "source" files:

Copyright Information

Every java source code file must contain a copyright information at the top of the file, i.e., before the package statement or (in case of absence) the import statement. The first line of the copyright information contains the name of the source code file. The version number as well as the date of last change (as preferred by Sun Micrososystems) need not be included.

    /* 
     * @(#)Table.java
     *
     * Copyright 2000 by <Name of person and/or organization>,  
     * All rights reserved.
     */

General Form of a Doc Comment

A doc comment is made up of two parts -- a description followed by zero or more tags, with a blank line (containing a single asterisk "*") between these two sections:

    /** 
     * This is the description part of a doc comment
     *
     * @tag    Comment for the tag
     */

Break any doc-comment lines exceeding 80 characters in length, if possible. If you have more than one paragraph in the doc comment, separate the paragraphs with a <p> paragraph tag.

Descriptions

First Sentence
The first sentence of each doc comment should be a summary sentence, containing a concise but complete description of the API. This sentence ends at the first period that is followed by a blank, tab, or line terminator, or at the first tag (as defined below). For example, this first sentence ends at "Prof.":

   /**
    * This is a simulation of Prof. Knuth's MIX computer.
    */

However, you can work around this by typing an HTML meta-character such as "&" or "<" immediately after the period, such as:

   /**
    * This is a simulation of Prof.&nbsp;Knuth's MIX computer.
    */

or

   /**
    * This is a simulation of Prof.<!-- --> Knuth's MIX computer.
    */

Javadoc copies this first sentence to the member summary at the top of the web page, so it is important to write crisp and informative initial sentences. In particular, write summary sentences that distinguish overloaded methods from each other. For example:

   /** 
    * Class constructor.
    */
   foo() {
     ...
    
   /**
    * Class constructor specifying number of objects to create.
    */
   foo(int n) {
     ...

Implementation-Independence
Write the description to be implementation-independent, but specifying such dependencies where necessary. This helps engineers write code to be "write once, run anywhere."

On Win32 systems, the path search behavior of the loadLibrary method is identical to that of the Win32 API's LoadLibrary procedure.

The use of "On Win32" at the beginning of the sentence makes it clear up front that this is an implementation note.

Automatic re-use of method comments
You can avoid re-typing doc comments by being aware of how Javadoc duplicates (inherits) comments for methods that override or implement other methods. This occurs in three cases:

In the first two cases, if a method m() overrides another method, Javadoc will generate a subheading "Overrides" in the documentation for m(), with a link to the method it is overriding.

In the third case, if a method m() in a given class implements a method in an interface, Javadoc will generate a subheading "Specified by" in the documentation for m(), with a link to the method it is implementing.

In all three of these cases, if the method m() contains no doc comments or tags, Javadoc will also copy the text of the method it is overriding or implementing to the generated documentation for m(). So if the documentation of the overridden or implemented method is sufficient, you do not need to add documentation for m(). If you add any documentation comment or tag to m(), the "Overrides" or "Specified by" subheading and link will still appear, but no text will be copied.

A Style Guide

The following are useful tips and conventions for writing descriptions in doc comments.

Tag Conventions

Most of the following tags are specified in the Java Language Specification. Also see the javadoc reference page.

Order of Tags
Include tags in the following order:

* @author       (classes and interfaces only, required)
* @version      (classes and interfaces only, required)
*               
* @param        (methods and constructors only)
* @return       (methods only)
* @exception    (@throws is a synonym added in Javadoc 1.2)
* @see          
* @spec		(classes only)
* @deprecated   
*
* @knownBugs    (classes, interfaces and methods only)
*
* @history      (classes and interfaces only)

Tag Blocks
For readability, divide the tags into blocks of related tags. The blocks shown above are an example.

Ordering Multiple Tags
We employ the following conventions when a tag appears more than once in a documentation comment. If desired, groups of tags, such as multiple @see tags, can be separated from the other tags by a blank line with a single asterisk.

Multiple @param tags should be listed in argument-declaration order. This makes it easier to match the list to the signature with your eyes.

Multiple @exception tags (also known as @throws) should be listed alphabetically by the exception names.

Multiple @see tags should be ordered as follows, which is roughly the same order as their arguments are searched for by javadoc, basically from nearest to farthest access, from least-qualified to fully-qualified, The following list shows this progression. Notice the methods and constructors are in "telescoping" order, which means the "no arg" form first, then the "1 arg" form, then the "2 arg" form, and so forth. Where a second sorting key is needed, they could be listed either alphabetically or grouped logically.

   @see #field
   @see #Constructor(Type, Type...)
   @see #Constructor(Type id, Type id...)
   @see #method(Type, Type,...)
   @see #method(Type id, Type, id...)
   @see Class
   @see Class#field
   @see Class#Constructor(Type, Type...)
   @see Class#Constructor(Type id, Type id)
   @see Class#method(Type, Type,...)
   @see Class#method(Type id, Type id,...)
   @see package.Class
   @see package.Class#field
   @see package.Class#Constructor(Type, Type...)
   @see package.Class#Constructor(Type id, Type id)
   @see package.Class#method(Type, Type,...)
   @see package.Class#method(Type id, Type, id)
   @see package

Required Tags
An @param tag is required for every paramete. It may be omitted in very obvious cases. The @return tag is required for every method that returns something other than void. It may be omited in very obvious cases.

Tag Comments
As a reminder, the fundamental use of these tags is described on the Javadoc Reference page. Java Software generally uses the following additional guidelines to create comments for each tag:

@author     (reference page)
An author is a person that has major responsibility for a class. There may be multiple authors for a class. Authors making contributions and enhancements to a class are listed with the @history tag.
@version     (reference page)
The version number is maintained manually. The initial version number to be used for documentation of the RSE Software is 1.0.
@param     (reference page)
The @param tag is followed by the name (not data type) of the parameter, followed by a description of the parameter. By convention, the first noun in the description is the data type of the parameter. (Articles like "a", "an", and "the" can precede the noun.) An exception is made for the primitive int, where the data type is usually omitted. Additional spaces can be inserted between the name and description so that the descriptions line up in a block. Dashes or other punctuation should not be inserted before the description, as Javadoc inserts one dash.

Parameter names are lowercase by convention. The data type starts with a lowercase letter to indicate an object rather than a class. The description is most usually a phrase, starting with a lowercase letter and ending without a period, unless it contains a complete sentence or is followed by another sentence (as described further below).

Example:

  * @param ch        the character to be tested
  * @param observer  the image observer to be notified 

Do not bracket the name of the parameter after the @param tag with <code>...</code> since Javadoc 1.2 automatically does this. (Javadoc will do the right thing and will not insert code tags around the parameter name if they are already present.)

When writing the comments themselves:

  • Prefer a phrase to a sentence.
  • Giving a phrase, do not capitalize, do not end with a period.
      @param x a phrase goes here
  • Giving a sentence, capitalize it and end it with a period.
      @param x This is a sentence.
  • When giving multiple sentences, follow all sentence rules.
      @param x This is sentence #1. This is sentence #2.
  • Giving multiple phrases, separate with a semi-colon and a space.
      @param x phrase #1 here; phrase #2 here
  • Giving a phrase followed by a sentence, do not capitalize the phrase. However, end it with a period to distinguish the start of the next sentence.
      @param x a phrase goes here. This is a sentence.
@return     (reference page)
Omit @return for methods that return void and for constructors; include it for all other methods, even if its content is entirely redundant with the method description. Having an explicit @return tag makes it easier for someone to find the return value quickly. Whenever possible, supply return values for special cases (such as specifying the value returned when an out-of-bounds argument is supplied).
@exception (@throws is a synonym added in Javadoc 1.2)     (reference page)
An @exception tag should be included for any checked exceptions (declared in the throws clause), as illustrated below, and also for any unchecked exceptions that the caller might reasonably want to catch, with the exception of NullPointerException. Errors should not be documented as they are unpredictable.
  /**
   * @exception IOException  If an input or output exception occurred
   */
  public void f() throws IOException {
      // body
  }
@spec    (This is not a standard java documentation tag)
Allows to place hints to the written specification. In most cases it will be sufficient to place the appropriate section number(s) of the written software requirements specification here. More text may be added.
@deprecated     (reference page)
The @deprecated description in the first sentence should at least tell the user when the API was deprecated and what to use as a replacement. Only the first sentence will appear in the summary section and index. Subsequent sentences can also explain why it has been deprecated. When generating the description for a deprecated API, Javadoc moves the @deprecated text ahead of the description, placing it in italics and preceding it with a bold warning: "Deprecated". An @see tag (for Javadoc 1.1) or {@link} tag (for Javadoc 1.2 or later) should be included that points to the replacement method. If the member has no replacement, the argument to @deprecated should be "No replacement".
@knownbugs     (This is not a standard java documentation tag)
Describes a known bug with the class or member function. One tag per bug should be used.
@history     (This is not a standard java documentation tag)
Describes how a class has been modified over time. For each change, the description should include: who made it, when it was made, what was done, why it was done, and a reference to the change request and/or user requirement that resulted in it. Minor bug fixes should not be included here.

Example:

  * @history 27.8.1999 Rainer Weinreich
  *          Method <code>public AgentDesc getAgentDesc()</code>inserted,as
  *          access is needed by several report agents.
  * @history 20.8.1999 Reinhold Plösch
  *          All instance variables of type jave.util.Vector changed to
  *          appropriate Java2-style collection classes.
{@link}   (Added in Javadoc 1.2)     (reference page)
For conventions, see Use In-Line Links Economically.