1002

How can I use the @link tag to link to a method?

I want to change:

/**
 * Returns the Baz object owned by the Bar object owned by Foo owned by this.
 * A convenience method, equivalent to getFoo().getBar().getBaz()
 * @return baz
 */
public Baz fooBarBaz()

to:

/**
 * Returns the Baz object owned by the Bar object owned by Foo owned by this.
 * A convenience method, equivalent to {@link getFoo()}.{@link getBar()}.{@link getBaz()}
 * @return baz
 */
public Baz fooBarBaz()

but I don't know how to format the @link tag correctly.

Arsen Khachaturyan
  • 7,904
  • 4
  • 42
  • 42
Jason S
  • 184,598
  • 164
  • 608
  • 970
  • 32
    I know this is a few years ago but... looking at the official Java classes can help find any form of Javadoc formatting you need. For example look at the HashMap Javadoc. – Christophe Roussy Mar 05 '14 at 15:56

3 Answers3

1275

You will find much information about JavaDoc at the Documentation Comment Specification for the Standard Doclet, including the information on the

{@link module/package.class#member label}

tag (that you are looking for). The corresponding example from the documentation is as follows

For example, here is a comment that refers to the getComponentAt(int, int) method:

Use the {@link #getComponentAt(int, int) getComponentAt} method.

The module/package.class part can be ommited if the referred method is in the current class.


Other useful links about JavaDoc are:

FrVaBe
  • 47,963
  • 16
  • 124
  • 157
880

The general format, from the @link section of the javadoc documentation, is:

{@link package.class#member label}

Examples

Method in the same class:

/** See also {@link #myMethod(String)}. */
void foo() { ... }

Method in a different class, either in the same package or imported:

/** See also {@link MyOtherClass#myMethod(String)}. */
void foo() { ... }

Method in a different package and not imported:

/** See also {@link com.mypackage.YetAnotherClass#myMethod(String)}. */
void foo() { ... }

Label linked to method, in plain text rather than code font:

/** See also this {@linkplain #myMethod(String) implementation}. */
void foo() { ... }

A chain of method calls, as in your question. We have to specify labels for the links to methods outside this class, or we get getFoo().Foo.getBar().Bar.getBaz(). But these labels can be fragile during refactoring -- see "Labels" below.

/**
 * A convenience method, equivalent to 
 * {@link #getFoo()}.{@link Foo#getBar() getBar()}.{@link Bar#getBaz() getBaz()}.
 * @return baz
 */
public Baz fooBarBaz()

Labels

Automated refactoring may not affect labels. This includes renaming the method, class or package; and changing the method signature.

Therefore, provide a label only if you want different text than the default.

For example, you might link from human language to code:

/** You can also {@linkplain #getFoo() get the current foo}. */
void setFoo( Foo foo ) { ... }

Or you might link from a code sample with text different than the default, as shown above under "A chain of method calls." However, this can be fragile while APIs are evolving.

Type erasure and #member

If the method signature includes parameterized types, use the erasure of those types in the javadoc @link. For example:

int bar( Collection<Integer> receiver ) { ... }

/** See also {@link #bar(Collection)}. */
void foo() { ... }
Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
  • Wait: I just want the method name with a link, I don't want the class name as well. – Jason S May 06 '11 at 19:25
  • Ah, okay. The first example in the link above illustrates that. – Andy Thomas May 06 '11 at 19:40
  • 1
    +1 for providing a Java 6 link that I was not linked to from the Oracle JavaDoc HowTo page. I still can't get along with the oracle links... (fixed links to Java 6 in my answer). – FrVaBe May 06 '11 at 19:46
  • @K. Claszen: http://download.oracle.com/javase/6/docs/, then click [javadoc](http://download.oracle.com/javase/6/docs/technotes/guides/javadoc/index.html) in the diagram, then choose [Javadoc Tool Reference Page (Microsoft Windows)](http://download.oracle.com/javase/6/docs/technotes/tools/windows/javadoc.html), then [Javadoc tags](http://download.oracle.com/javase/6/docs/technotes/tools/windows/javadoc.html#javadoctags). – Paŭlo Ebermann May 06 '11 at 23:16
  • @Paŭlo Ebermann Thanks! Will try to use the 'docs' page as entry point in the future. – FrVaBe May 07 '11 at 05:52
  • @zasadnyy - Thank you for your attempt to add the important note about generics. I liked it, but didn't get to the review before others. I've added the section *#member and generics* to the answer above. – Andy Thomas Dec 09 '15 at 15:47
  • The label keeps the link short, by hiding package and class information. That's great! – Jose Tepedino Apr 07 '19 at 18:36
  • @JoseTepedino - The label is harmful, if refactoring may occur -- and refactoring is very easy these days, for code not shared with third parties. – Andy Thomas Apr 07 '19 at 18:59
18

you can use @see to do that:

sample:

interface View {
        /**
         * @return true: have read contact and call log permissions, else otherwise
         * @see #requestReadContactAndCallLogPermissions()
         */
        boolean haveReadContactAndCallLogPermissions();

        /**
         * if not have permissions, request to user for allow
         * @see #haveReadContactAndCallLogPermissions()
         */
        void requestReadContactAndCallLogPermissions();
    }
vuhung3990
  • 6,353
  • 1
  • 45
  • 44
  • 6
    OP: "How can I use the @link tag to link to a method?" The *@link* tag can be used inline within a paragraph, as requested by the OP. The *@see* tag cannot. See more detail at [this question](http://stackoverflow.com/questions/10097199/javadoc-see-or-link). – Andy Thomas Jan 13 '17 at 00:08