147

Using Javadoc 1.5, I have been unable to create a @link to an Enumeration value.

What I would like to do is to create an Enum like this:

public enum Planet { 

/**
* MERCURY is a fun place.
*/
MERCURY, 

/**
* VENUS is more fun.
*/
VENUS, 

/**
* But nothing beats the Earth.
*/
EARTH,

/**
* Others we know nothing about.
*/ 
OTHERS
}

And then refer to the Javadoc for Earth using a link like this:

{@link Planet.EARTH}

I have tried the {@link Planet#EARTH} style too, but to no avail.

Anyone know if this is doable at all?

3 Answers 3

239

The # style works for me:

{@link Planet#EARTH}

The key is that the Planet package must be imported, or Planet must be fully qualified - i.e.:

{@link com.yourpackage.Planet#EARTH}
5
  • As sfussenegger noted, Eclipse handles the import for you.
    – aperkins
    Oct 5, 2009 at 17:21
  • Thanks both answers helpful! I did get it to work using the fully qualified reference. Sometimes the compiler output isn't really helpful in determining what the problem is... Oct 5, 2009 at 17:28
  • 3
    What if enum is defined within class A and one is trying to reference one of its values from class B javadoc? I've found neither A.Planet#EARTH nor A#Planet#EARTH to work for OpenJDK 1.6 update 24 javadoc, although eclipse knows to find declaration with A.Planet#EARTH style. Feb 12, 2012 at 17:46
  • Unfortunately that doesn't work if you have a static import of enum. At least in intellij idea there is no way to use a statically imported enum in javadoc, unless you use the fully qualified enum name with packages etc.
    – dhblah
    May 29, 2015 at 13:47
  • This however does not work with static import. Having import static com.yourpackage.Planet.EARTH I can not simply use {@link EARTH} even though Intellij Idea (2023.2.2) suggests it. It glows red afterwards. Sep 14 at 10:41
7

I'm using Eclipse to check this, but

{@link Planet#EARTH}

style seems to work. However, I normally prefer

@see Planet#EARTH

anyway. Not sure what Eclipse uses to generate Javadoc, but I'm using JDK6. Still, maybe @see does the trick for you.

1
  • 4
    I like @see but sometimes you need special cases. For example, my orders have an isWithdrawn() method, and I specifically say @return true if the status of this order is equal to OrderStatus#WITHDRAWN, false otherwise
    – corsiKa
    Oct 3, 2013 at 17:21
2

As long as it's imported you can link it (but when you do this, IMO it makes the imports messy- what ones are used in code and what ones in javadoc? I like to just use the fully qualified name).

But yes, Eclipse can take care of it all and standard

{@link Planet#EARTH}

works fine.

If your using Eclipse, Ctrl + Shift + O (on PC) or Cmd + Shift + O (on Mac) auto-adjust your imports (this means if you have extra imports not being used, they're removed, as well as adding any imports you need).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.