Java Language


Documenting Java Code All Versions

Java SE 1.0
Java SE 1.1
Java SE 1.2
Java SE 1.3
Java SE 1.4
Java SE 5
Java SE 6
Java SE 7
Java SE 8
Java SE 9 (Early Access)

This draft deletes the entire topic.

expand all collapse all

Examples

  • 8

    Many IDEs provide support for generating HTML from Javadocs automatically; some build tools (Maven and Gradle, for example) also have plugins that can handle the HTML creation.

    However, these tools are not required to generate the Javadoc HTML; this can be done using the command line javadoc tool.

    The most basic usage of the tool is:

    javadoc JavaFile.java
    

    Which will generate HTML from the Javadoc comments in JavaFile.java.

    A more practical use of the command line tool, which will recursively read all java files in [source-directory], create documentation for [package.name] and all sub-packages, and place the generated HTML in the [docs-directory] is:

    javadoc -d [docs-directory] -subpackages -sourcepath [source-directory] [package.name]
    
  • 15

    All Javadoc comments begin with a block comment followed by an asterisk (/**) and end when the block comment does (*/). Optionally, each line can begin with arbitrary whitespace and a single asterisk; these are ignored when the documentation files are generated.

    /**
     * Brief summary of this class, ending with a period.
     *
     * It is common to leave a blank line between the summary and further details.
     * The summary (everything before the first period) is used in the class or package 
     * overview section.
     *
     * The following inline tags can be used (not an exhaustive list): 
     * {@link some.other.class.Documentation} for linking to other docs or symbols
     * {@code code goes here} for formatting as code
     * {@literal <>[]()foo} for interpreting literal text without converting to HTML markup 
     * or other tags.
     *
     * Optionally, the following tags may be used at the end of class documentation 
     * (not an exhaustive list):
     *
     * @author John Doe
     * @version 1.0
     * @since 5/10/15
     * @see some.other.class.Documentation
     * @deprecated This class has been replaced by some.other.package.BetterFileReader
     */
    public class FileReader {
    }
    

    The same tags and format used for Classes can be used for Enums and Interfaces as well.

  • 10

    All Javadoc comments begin with a block comment followed by an asterisk (/**) and end when the block comment does (*/). Optionally, each line can begin with arbitrary whitespace and a single asterisk; these are ignored when the documentation files are generated.

    /**
     * Brief summary of method, ending with a period.
     *
     * Further description of method and what it does, including as much detail as is 
     * appropriate.  Inline tags such as
     * {@code code here}, {@link some.other.Docs}, and {@literal text here} can be used.
     *
     * If a method overrides a superclass method, {@inheritDoc} can be used to copy the 
     * documentation
     * from the superclass method
     *
     * @param stream Describe this parameter.  Include as much detail as is appropriate
     *               Parameter docs are commonly aligned as here, but this is optional.
     *               As with other docs, the documentation before the first period is 
     *               used as a summary.
     *
     * @return Describe the return values.  Include as much detail as is appropriate
     *         Return type docs are commonly aligned as here, but this is optional.
     *         As with other docs, the documentation before the first period is used as a 
     *         summary.
     *
     * @throws IOException Describe when and why this exception can be thrown.
     *                     Exception docs are commonly aligned as here, but this is 
     *                     optional.
     *                     As with other docs, the documentation before the first period 
     *                     is used as a summary.
     *                     Instead of @throws, @exception can also be used.
     *
     * @since 2.1.0
     * @see some.other.class.Documentation
     * @deprecated  Describe why this method is outdated. A replacement can also be specified.
     */
    public String[] read(InputStream stream) throws IOException {
        return null;
    }
    

I am downvoting this example because it is...

Syntax

  • /** -- start of JavaDoc on a class, field, method, or package
  • @author // To name the author of the class, interface or enum. It is required.
  • @version // The version of that class, interface or enum. It is required. You could use macros like %I% or %G% for you source control software to fill in on checkout.
  • @param // To show the arguments (parameters) of a method or a constructor. Specify one @param tag for each parameter.
  • @return // To show the return types for non-void methods.
  • @exception // Shows what exceptions could be thrown from the method or constructor. Exceptions that MUST be caught should be listed here. If you want, you can also include those that do not need to be caught, like ArrayIndexOutOfBoundsException. Specify one @exception for each exception that can be thrown.
  • @throws // Same as @exception.
  • @see // Links to a method, field, class or package. Use in the form of package.Class#something.
  • @since // When this method, field or class was added. For example, JDK-8 for a class like java.util.Optional<T>.
  • @serial, @serialField, @serialData // Used to show the serialVersionUID.
  • @deprecated // To mark a class, method or field as deprecated. For example, one would be java.io.StringBufferInputStream. See a full list of existing deprecated classes here.
  • {@link} // Similar to @see, but can be used with custom text: {@link #setDefaultCloseOperation(int closeOperation) see JFrame#setDefaultCloseOperation for more info}.
  • {@linkplain} // Similar to {@link}, but without the code font.
  • {@code} // For literal code, such as HTML tags. For example: {@code <html></html>}. However, this will use a monospaced font. To get the same result without the monospace font, use {@literal}.
  • {@literal} // Same as {@code}, but without the monospaced font.
  • {@value} // Shows the value of a static field: The value of JFrame#EXIT_ON_CLOSE is {@value}. Or, you could link to a certain field: Uses the app name {@value AppConstants#APP_NAME}.
  • {@docRoot} // The root folder of the JavaDoc HTML relative to the current file. Example: <a href="{@docRoot}/credits.html">Credits</a>.
  • HTML is allowed: <code>"Hi cookies".substring(3)</code>.
  • */ -- end of JavaDoc declaration

Parameters

Parameters

Remarks

Javadoc is a tool included with the JDK that allows in-code comments to be converted to an HTML documentation. The Java API Specification was generated using Javadoc. The same is true for much of the documentation of 3rd-party libraries.

Still have a question about Documenting Java Code? Ask Question

Topic Outline