21

I have two Java functions:

/**
* Do something with param
*/
public String doSomething(String param) {...};

/**
* ...
*/
public String doSomething(Integer param) {...};

How can I make the second function's description to show an exact copy of the first function?

RoflcoptrException
  • 51,941
  • 35
  • 152
  • 200
GetUsername
  • 1,057
  • 5
  • 18
  • 29

5 Answers5

23

Assuming copy and paste won't work for you, I believe the convention is to use the @see tag to refer to another method which will give greater detail.

In your example the doSomething(Integer param) would have an @see tag referring to the String version.

Wikipedia has some examples, http://en.wikipedia.org/wiki/Javadoc

As does the oracle site for the javadoc tool http://www.oracle.com/technetwork/java/javase/documentation/index-137868.html#multiple@see

Kevin D
  • 3,564
  • 1
  • 21
  • 38
9

The short answer is you can't. Customary is to make use of the @see directive or simply copy pasting.

If you are subclassing you can put the javadoc on the interface level instead to achieve what you want.

Johan Sjöberg
  • 47,929
  • 21
  • 130
  • 148
6

As two methods with different type params can't have the same description. But for inherited method we can use same description.

inherited method

For inherited method u can use

{@inheritDoc}

It copies the description from the overridden method.

Abhishek
  • 2,095
  • 2
  • 21
  • 25
2

You don't want to do that. You want the second one to refer to the first one. That's what @see is for. You never want to repeat documentation, for the same reason that your second method calls the first method instead of containing a copy of its code.

user207421
  • 305,947
  • 44
  • 307
  • 483
1

Don't just use {@see ...}, which has a different meaning and has some problems (like not overriding inherited documentation).

A spartan /** See: {@link ...}. */ is better.

However, best is to add a bit more text than just "See". Briefly describe the intent of this method and what is specific to it, and {@link ...} to a method which explains the full contract in detail. This is often done in the JDK and other libraries and is a good practice.

Eg:

/**
 * Does something very important.
 * For details see {@link #doSomething(Integer)}.
 */

or:

/**
 * Does something very important.
 * Equivalent to calling {@link #doSomething(Integer) doSomething(0)}.
 */
Aleksandr Dubinsky
  • 22,436
  • 15
  • 82
  • 99