51

Suppose your class has 2 methods:

contains() and
containsSame()

The distinction between them is subtle and you'd like to mention it as part of Javadoc

In Javadoc, how can you reference a method in the same class, by name?

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
James Raitsev
  • 92,517
  • 154
  • 335
  • 470
  • possible duplicate of [javadoc: writing links to methods](http://stackoverflow.com/questions/5915992/javadoc-writing-links-to-methods) – Simon Nov 18 '13 at 14:56

1 Answers1

85

Use the @link inline tag, and refer to the method with a leading #.

/**
 * ...
 * This method is similar to {@link #contains()}, with the following differences:
 * ...
 */
public boolean containsSame();


/**
 * This method does ...
 */
public boolean contains();

This example only works if there is actually a contains() method which has no arguments (which, actually, seems to be not that useful). If you have only a contains method with arguments, then either write the argument types in the parentheses:

/**
 * ...
 * This method is similar to {@link #contains(Element)}, with the following differences:
 * ...
 */
public boolean containsSame(Element e);

/**
 * This method does ...
 */
public boolean contains(Element e);

Or you can omit the parentheses completely:

/**
 * ...
 * This method is similar to {@link #contains}, with the following differences:
 * ...
 */
public boolean containsSame(Element e);

/**
 * This method does ...
 */
public boolean contains(Element e);

If you have several methods named contains (with different parameter lists), this version can't decide which one to use (the link will jump to any of them, hopefully they are all together and do similar things).

Ryan Haining
  • 35,360
  • 15
  • 114
  • 174
Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
  • Just an FYI: I was getting a warning with the above answer. Removing the parenthesis on the method fixed them: {@link #contains} – Pytry Jun 05 '15 at 14:41
  • 1
    @Pytry I guess your method actually had parameters? I updated the answer to reflect this. – Paŭlo Ebermann Jun 05 '15 at 18:21
  • *facepalms* ... You're right that I was. Your expanded explanation is great. Thanks again! – Pytry Jun 08 '15 at 16:31