Why Document OOP Code?
| Reason | Explanation |
|---|---|
| Onboarding | New developers understand classes without reading every line |
| Contract | Javadoc defines what a method promises, not how it works |
| API design | Public APIs are useless without clear documentation |
| Tooling | IDEs show Javadoc in tooltips; javadoc generates HTML |
Javadoc Basics
/**
* Represents a bank account with basic deposit and withdrawal operations.
*
* <p>Thread-safety: this class is <b>not</b> thread-safe.
*
* @author Sushil Verma
* @since 1.0
*/
public class BankAccount {
// ...
}
Key rules:
- Start with a summary sentence (first sentence ends at the first period).
- Use
<p>for additional paragraphs. - Place
@author,@since,@seeafter the description.
Documenting Methods
/**
* Transfers money from this account to the target account.
*
* <p>Both accounts are updated atomically. If the transfer fails,
* neither balance is changed.
*
* @param target the destination account (must not be {@code null})
* @param amount the amount to transfer (must be positive)
* @return {@code true} if the transfer succeeded
* @throws IllegalArgumentException if {@code amount} is not positive
* @throws InsufficientFundsException if balance is less than {@code amount}
* @see #withdraw(double)
*/
public boolean transfer(BankAccount target, double amount) {
// ...
return true;
}
Common Tags
| Tag | Purpose |
|---|---|
@param name desc |
Describe each parameter |
@return desc |
Describe the return value |
@throws ExType desc |
Document each checked/unchecked exception |
@see #method |
Cross-reference related members |
@deprecated |
Mark and explain deprecation |
{@code text} |
Inline code |
{@link Class#method} |
Hyperlinked reference |
Documenting Interfaces
/**
* A strategy for calculating shipping costs.
*
* <p>Implementations must be stateless and thread-safe.
*
* @param <T> the type of item being shipped
*/
public interface ShippingStrategy<T> {
/**
* Calculates the shipping cost for the given item.
*
* @param item the item to ship (never {@code null})
* @return the cost in USD, always ≥ 0
*/
double calculate(T item);
}
Document the contract — what implementors must guarantee.
Documenting Enums
/**
* HTTP status code families.
*/
public enum StatusFamily {
/** 1xx — Informational responses. */
INFORMATIONAL,
/** 2xx — Successful responses. */
SUCCESS,
/** 4xx — Client error responses. */
CLIENT_ERROR,
/** 5xx — Server error responses. */
SERVER_ERROR
}
Package-Level Documentation
Create package-info.java in each package:
/**
* Core domain model for the banking application.
*
* <p>This package contains entity classes like {@link BankAccount},
* {@link Transaction}, and {@link Customer}.
*
* @since 1.0
*/
package com.example.banking.model;
Generating HTML Docs
# Generate Javadoc for all source files
javadoc -d docs/api -sourcepath src/main/java -subpackages com.example
# With Maven
mvn javadoc:javadoc
The output is a browsable HTML site — ideal for publishing internally or alongside your JAR.
Documentation Best Practices
- Document the what and why, not the how — implementation details belong in code comments.
- Every public class and method must have Javadoc.
- Use
@paramfor every parameter — even if the name seems obvious. - State preconditions and postconditions — what must be true before and after calling.
- Keep docs in sync — outdated docs are worse than no docs.
Bad vs Good Documentation
// ❌ BAD — restates the code
/** Sets the name. */
public void setName(String name) { this.name = name; }
// ✅ GOOD — adds value
/**
* Updates the display name shown in the UI.
*
* @param name the new display name; must be 1-100 characters
* @throws IllegalArgumentException if name is blank or too long
*/
public void setName(String name) { /* ... */ }
Key Takeaways
- Javadoc is a contract, not a code walkthrough.
- Use standard tags —
@param,@return,@throws— consistently. - Document interfaces thoroughly — they define the rules for implementors.
- Use
package-info.javafor package-level context. - Generate and publish HTML docs with
javadocor Maven.
What’s Next?
🎉 Congratulations! You’ve completed the Object-Oriented Programming in Java tutorial series. You now have a solid foundation in OOP concepts, design principles, and practical skills. Continue your Java journey with the next module!