Explain Codes LogoExplain Codes Logo

How to @link to an Enum Value using Javadoc

java
javadoc
enum
best-practices
Anton ShumikhinbyAnton Shumikhin·Jan 27, 2025
TLDR

To create a link to a specific enum value in Javadoc, the format is {@link EnumType#ENUM_VALUE}, here's a quick example for you:

/** * Uses {@link DayOfWeek#SUNDAY} to check if we're `still in bed` mode on. */ public void checkWeekend() { // Any further lines of code will only be executed // if the duvet permits it 😴 }

Simply replace DayOfWeek with your enum type and SUNDAY with the value you're targeting.

Providing correct context to Enums

When the enum you're referring isn't imported, make sure to specify the fully qualified name:

/** * Back to work after the weekend: * {@link java.time.DayOfWeek#MONDAY}! */

This ensures that the Javadoc tool knows exactly what you're referring to. This is particularly useful when sharing code documentation externally.

If you're an Eclipse user, don't forget:

  • Ctrl + Shift + O (PC) or Cmd + Shift + O (Mac) to automatically manage imports, dominate your workspace. 🚀

Enum values referencing: best practices

  • Consistency: Adopt the same style across the board.
  • Testing: Keep an eye out for those generated Javadoc links.
  • Compatibility: Ensure your JDK version and @link work together, like a power duo.

Instead of @link, you might find @see fits better in some contexts:

/** * Main method. * @see DayOfWeek#MONDAY because the real fun begins when going back to work, right? */ public static void main(String[] args) { // Why couldn't the programmer go to work? // Because they didn't "get" arrays! 😉 }

The @see creates a separate section in the Javadoc under "See Also". Quite handy for pointing readers to related info.

Troubleshooting, or doing the code detective gig

Keep an eye out for these common culprits:

  • Broken links: Verify that the enum value exists and is public.
  • Javadoc generation issues: Make sure you've been using Javadoc properly.
  • Outdated references: Align the documentation with code changes - yes, like matching socks!

Going a step further

For additional context within your documentation, remember to:

  • Include descriptive context around links for better sightseeing.
  • Elucidate the linked item's usage or relationship within your documentation.