Javadoc Link tag example | Java Documentation example

This tutorial explains @link tag usage in java documentation.

@link tag used to create anchor link, point to classes, methods, fields of same or different package.

It is used to navigate from one place to other place with in java components.

Here is an syntax.

/**
 * {@link package.class#field}
 * {@link package.class#method(parameter)}
 */

package: package name class: class names field: variables or constants method: method name

You can also add `target="_top"` to the anchor tag to replace and load external URLs on a separate browser page instead of a single frame on the Javadoc web page.


```java

// Example 1
/**
 * It calls {@link java.util.List#size()} method
 * to List size.
 *
 * @param List Find the list size.
 */
public void getSize(List[] list) {
  return list.size()
}

// Example 2
/**
 * Employee class contains holds.
 * See {@link package.Employee} for more details.
 */
public class Employee {
}


// Example 3 Linking to variables
/**
 * Employee class contains holds.
 * See {@link package.Employee.id} for more details.
 */
public class Employee {
      public Integer id;
}


// Example 4 Linking to method

public class sales {
/**
 * This method Calculates total sales for a all years
 * {@link com.erp.sales}.
 *
 * @param year An Array of integer value.
 * @return The total sales for all years.
 */
public double getTotalSales(years) {
    return salesService.getTotalSales(years);
}}

How to reference a method in javadoc?

Here is an syntax used to link a method

/**
 * {@link package.class#method(parameter)}
 */

method can be from same class or another class of package It can also include arguments.


// Example 4 Linking to method

public class Sales {
/**
 * This method Calculates total sales for a all years
 * {@link com.erp.sales.SalesService#getTotalSales(int[])}.
 *
 * @param year An Array of integer value.
 * @return The total sales for all years.
 */
public double getTotalSales(years) {
    return salesService.getTotalSales(years);
}}