Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Documenting with Javadoc How to Write Doc Comments for the Javadoc TM Tool available from java.sun.com.

Similar presentations


Presentation on theme: "1 Documenting with Javadoc How to Write Doc Comments for the Javadoc TM Tool available from java.sun.com."— Presentation transcript:

1 1 Documenting with Javadoc How to Write Doc Comments for the Javadoc TM Tool available from java.sun.com.

2 2 Overview  Need for Javadoc  Writing Javadoc comments  Running the javadoc tool

3 3 Need for Javadoc  Why document programs? To make it easy to understand, e.g., for reuse and maintenance  What to document? Interface: syntactic vs. semantic (or behavioral) interfaces Internal working

4 4 (Cont.)  Why Javadoc? To combine source code with documentation and other reference materials To make it easier to keep the documentation and code in sync To generate API specifications (or interface specifications) from source code

5 5 Approach  Javadoc comments Attach special comments, called documentation comment (or doc comment) to classes, fields, and methods. /** … */  Javadoc tool Use a tool, called javadoc, to automatically generate HTML pages from source code.

6 6 Javadoc Example /** An abstract class representing various kinds of shapes. */ public abstract class Shape { /** The x-coordinate of this shape. */ private int x; // … /** Returns the x-coordinate of this shape. */ public int getX() { … } /** Sets the x-coordinate of this shape to the argument * x. */ public void setX(int x) { … } // … }

7 7 Javadoc Tags  Javadoc Tags Special keyword recognized by javadoc tool. Will be specially formatted  Common Tags @authorAuthor of the feature @versionCurrent version number @sinceSince when @paramMeaning of parameter @returnMeaning of return value @throwsMeaning of exception @seeLink to other features

8 8 Example /** An abstract class representing various kinds of * shapes. This class represents common features * of all shapes such as … * * @author Yoonsik Cheon * @version 1.0 (01/22/04) * @since version 0.5 */ public abstract class Shape { // … }

9 9 Specifying Parameters and Return Value  Syntax @param name description @return description @throws exception description  Example /** Returns the definition of a given word in this dictionary. * * @param word a word whose definition is being looked up. * @return the definition of the word; null if no definition is * found. * @throws NullPointerException if the word is null. */ public String lookup(String word) { /* … */ }

10 10 Linking to Other Features  Syntax @see featureName where featureName is class, field, or method.  Example @see Dictionary @see #elems @see #lookup(String) @see SpanishDictionary#lookup(String) @see cs3331.Score#lookup(String)

11 11 Exercise  Write Javadoc comments for the following class. public class ArrayStack { public ArrayStack(int size) { … } public Object push(Object elem) { … } public Object pop() throws StackEmptyException { … } private Object[] elems; private int idx; }

12 12 Running javadoc Tool  Similar to running javac, e.g., javadoc A.java javadoc A.java B.java javadoc *.java javadoc –d javadocs *.java

13 13 Running javadoc (Cont.)  Linking to existing API specifications javadoc –link file:c:/Software/jdk1.6.0/docs/api *.java javadoc –link http://java.sun.com/j2se/1.6.0/docs/api *.java  Including non-public features Javadoc –private *.java


Download ppt "1 Documenting with Javadoc How to Write Doc Comments for the Javadoc TM Tool available from java.sun.com."

Similar presentations


Ads by Google