Part 10 / Topic 10 of 12
Javadoc
A Javadoc comment (/** ... */, starting with two stars) documents a public class or method so both an IDE's tooltip and the javadoc tool (part of the JDK, no install needed) can read it.
/**
* Withdraws {@code amount} from the account.
*
* @param amount the amount to withdraw; must be positive and no more than the balance
* @throws IllegalArgumentException if {@code amount} is zero or negative
* @throws InsufficientFundsException if {@code amount} is more than the current balance
*/
public void withdraw(double amount) { ... }
{@code amount} renders amount in a code font in the generated HTML; @param documents one parameter, @throws documents one checked-or-unchecked exception the method can throw. Run the tool for real, from inside examples/bank-and-discount/ (it needs the JUnit jar on its classpath too, purely to resolve the test classes sitting in the same folder — javadoc does not generate pages for files you did not name, but it still parses every .java file in the same directory to build the full picture):
$ javadoc -d /tmp/bank-account-docs -classpath ../lib/junit-platform-console-standalone-1.14.5.jar BankAccount.java AlertService.java InsufficientFundsException.java
Loading source file BankAccount.java...
Loading source file AlertService.java...
Loading source file InsufficientFundsException.java...
Constructing Javadoc information...
Building index for all the packages and classes...
Standard Doclet version 21.0.11+10-LTS
Building tree for all the packages and classes...
Generating /tmp/bank-account-docs/AlertService.html...
Generating /tmp/bank-account-docs/BankAccount.html...
Generating /tmp/bank-account-docs/InsufficientFundsException.html...
...
Leave out -classpath ... and javadoc fails instead, with the exact same "package ... does not exist" errors javac would give for the same missing jar — javadoc compiles enough of your code to understand it before it can document it. Leave out a comment on a public method and javadoc does not fail, but it does warn: a first draft of BankAccount with no comment on deposit prints warning: no comment at that exact line — a cheap, free "you forgot to document this" check, worth running before anyone else reads your public API.
Full programs: examples/bank-and-discount/BankAccount.java, examples/bank-and-discount/AlertService.java, examples/bank-and-discount/InsufficientFundsException.java.
Done here? The practice set for Part 10 — including the solutions — is on the part page.