Showing posts with label javadoc tags. Show all posts
Showing posts with label javadoc tags. Show all posts

Saturday, 20 January 2018

JavaDoc: @value: Display constant values

@value tag is used to display constant values.

The values of all the constants that are tagged with @value tag are displayed in 'Constant Field Values' section.

Arithmetic.java

package com.java.tags;

/**
 * 
 * @author Hari Krishna
 * 
 * @since 1.0
 */
public interface Arithemtic {

 /**
  * The value of PI is {@value}
  */
 public static final double PI = 3.14;

 /**
  * 
  * @param radius
  *            Radius of the Circle
  * 
  * @return area of the circle.
  * 
  * @since 1.1
  */
 public double areaOfCircle(int radius);

}



Previous                                                 Next                                                 Home

Javadoc: @link: Used to create inline links

@link tag is used to create inline links that can refer to the documentation for the specified package, class, or member name of a referenced class.

Syntax
{@link package.class#member label}

Refer constant of same class
/**
 * Calculates the area of circle. It uses value of {@link #PI} in
 * calculation.
 *
 */
public double areaOfCircle(int radius);

Refer method of same class
/**
 * Prints the area of circle. It internally calls {@link #areaOfCircle(int)}
 * method
 *
 */
public void printAreaOfCircle(int radius);

Refer method of other class
/**
 * Calculate the squalre root of the parameter a. It internally calls
 * {@link java.lang.Math#sqrt(double)}
 *
 * @return square root of a
 */
public default double sqrt(double a) {
         return Math.sqrt(a);
}

Find the below example.


Arithmetic.java
package com.java.tags;

/**
 * 
 * @author Hari Krishna
 * 
 * @since 1.0
 */
public interface Arithemtic {

 /**
  * The value of PI is {@value}
  */
 public static final double PI = 3.14;

 /**
  * Calculates the area of circle. It uses value of {@link #PI} in
  * calculation.
  * 
  * @param radius
  *            Radius of the Circle
  * 
  * @return area of the circle.
  * 
  * @since 1.1
  */
 public double areaOfCircle(int radius);

 /**
  * Prints the area of circle. It internally calls {@link #areaOfCircle(int)}
  * method
  * 
  * @param radius
  *            Radius of the Circle.
  */
 public void printAreaOfCircle(int radius);

 /**
  * Calculate the squalre root of the parameter a. It internally calls
  * {@link java.lang.Math#sqrt(double)}
  * 
  * @param a
  *            Operand
  * @return square root of a
  */
 public default double sqrt(double a) {
  return Math.sqrt(a);
 }

}



Previous                                                 Next                                                 Home

Javadoc: @inheritDoc: Inherit the documentation

{@inheritDoc} tag is used to inherit the documentation from the nearest inheritable class or implementable interface into the current documentation comment at this tag's location.

You can use @inheritedDoc tag in description block, or in the text arguments of @return, @param, and @throws tags.

Usage of @inheritDoc tag in description block
When you use @inheritDoc tag in description block, the main description is copied from a class or interface up the hierarchy.

/**
 * {@inheritDoc}
 */
@Override
public void welcomeEmployee(String empName) {
         System.out.println("Hello " + empName);
}

ABCOrg.java
package com.java.tags;

/**
 * 
 * @author Hari Krishna
 * 
 * @since 1.0
 */
public class ABCOrg {

 /**
  * Print welcome message to employee.
  * 
  * @param empName
  *            Employee Name
  */
 public void welcomeEmployee(String empName) {
  System.out.println("Hello " + empName);
 }

}

XYZOrg.java

package com.java.tags;

public class XYZOrg extends ABCOrg{

 /**
  * {@inheritDoc}
  */
 @Override
 public void welcomeEmployee(String empName) {
  System.out.println("Hello " + empName);
 }
}


Javadoc for the class XYZOrg generated like below.

Usage of @inheritDoc tag in tag descriptions of @return, @param, and @throws
When you use @inheritDoc tag in tag descriptions of @return, @param and @throws tags, the tag text is copied from the corresponding tag up the hierarchy.

ABCOrg.java
package com.java.tags;

/**
 * 
 * @author Hari Krishna
 * 
 * @since 1.0
 */
public class ABCOrg {

 /**
  * Print welcome message to employee.
  * 
  * @param empName
  *            Employee Name
  */
 public void welcomeEmployee(String empName) {
  System.out.println("Hello " + empName);
 }

}


XYZOrg.java
package com.java.tags;

public class XYZOrg extends ABCOrg{

 /**
  * Welcome Employee.
  * 
  * @param empName {@inheritDoc}
  */
 @Override
 public void welcomeEmployee(String empName) {
  System.out.println("Hello " + empName);
 }
}


Javadoc for the class XYZOrg generated like below.



Previous                                                 Next                                                 Home

Javadoc: @version: Specify current version of the release

@version tag specify the current version of the release.

Syntax
@version version-text


/**
 * 
 * @author Hari Krishna
 * 
 * @version 1.5
 * 
 * @since 1.2
 *
 */
public class ABCOrg {

}


Javadoc is generated like below.

Previous                                                 Next                                                 Home

JavaDoc: @throws: specify the exception thrown by method

@throws tag is used to specify the exception thrown by a method (or) constructor.

Syntax
@throws class-name description

Can I use multiple throws tags to specify different exceptions?
Yes

Example
/**
 *
 * @throws IllegalArgumentException
 *             thrown if radius < 0.
 */
public double areaOfCircle(int radius) throws IllegalArgumentException;

Arithmetic.java
package com.java.tags;

/**
 * 
 * @author Hari Krishna
 * 
 * @since 1.0
 */
public interface Arithmetic {

 /**
  * The value of PI is {@value}
  */
 public static final double PI = 3.14;

 /**
  * 
  * @param radius
  *            radius of the circle
  * 
  * @return area of the circle
  * 
  * @throws IllegalArgumentException
  *             thrown if radius < 0.
  */
 public double areaOfCircle(int radius) throws IllegalArgumentException;

}

javadoc is generated like below.



Previous                                                 Next                                                 Home

Javadoc: @since tag

@since tag is used to specify, this feature has existed since specific software release.

Syntax
@since since-text

Where can I use @since tag?
You can use on package, class, interface, constructor, method, or field.

Arithmetic.java
package com.java.tags;

/**
 * 
 * @author Hari Krishna
 *
 * @param <T>
 *            Type of element to perform Arithmetic operations
 * 
 * @since 1.0
 */
public interface Arithemtic<T extends Number> {

 /**
  * 
  * @param a
  *            First Operand
  * @param b
  *            Second Operand
  * 
  * @return sum of two operands a and b
  * 
  * @since 1.1
  * 
  */
 T sum(T a, T b);
}




Previous                                                 Next                                                 Home

Javadoc: @serial: Document meaning of serial fields

@serial tag is used to document the acceptable values and description of serialized fields.

Javadoc tool places this information in the "Serialized Form" page for that class

Ex
/**
 * @serial Serialize PI {@value}
 */
public final double PI = 3.14;

Arithmetic.java

package com.java.tags;

import java.io.Serializable;

/**
 * 
 * @author Hari Krishna
 * 
 * @since 1.0
 */
public class Arithmetic implements Serializable {

 /**
  * Serialization constant value is {@value}
  */
 private static final long serialVersionUID = 1L;

 /**
  * @serial Serialize PI {@value}
  */
 public final double PI = 3.14;

 public double areaOfCircle(int radius) {
  return PI * radius * radius;
 }

}





Previous                                                 Next                                                 Home

Javadoc: @see: Document other references

It adds a "See Also" heading with a link or text entry that points to a reference.

Syntax
@see reference

/**
 *
 * @see Math#sqrt(double)
 */
public double sqrt(double number) throws IllegalArgumentException;

Arithmetic.java

package com.java.tags;

/**
 * 
 * @author Hari Krishna
 * 
 * @since 1.0
 */
public interface Arithmetic {

 /**
  * 
  * @param number
  *            number to calculate square root for
  * 
  * @return square root of a number
  * 
  * @throws IllegalArgumentException
  *             if number <= 0
  * 
  * @see Math#sqrt(double)
  */
 public double sqrt(double number) throws IllegalArgumentException;

}

javadoc is generated like below.


Where can I use @see tag?
You can use on top of package, class, interface, constructor, method, or field

What is the difference between {@link} and @see tags?
{@link} tag generates an inline link rather than placing the link in the See Also section

How to reference a member of the current class?
@see #field
@see #method(Type, Type,...)
@see #method(Type argname, Type argname,...)
@see #constructor(Type, Type,...)
@see #constructor(Type argname, Type argname,...)

How to reference a member of other class?
@see Class#field
@see Class#method(Type, Type,...)
@see Class#method(Type argname, Type argname,...)
@see Class#constructor(Type, Type,...)
@see Class#constructor(Type argname, Type argname,...)
@see Class.NestedClass
@see Class

How to reference a member of other package?
@see package.Class#field
@see package.Class#method(Type, Type,...)
@see package.Class#method(Type argname, Type argname,...)
@see package.Class#constructor(Type, Type,...)
@see package.Class#constructor(Type argname, Type argname,...)
@see package.Class.NestedClass
@see package.Class
@see package


Previous                                                 Next                                                 Home

Javadoc: @return tag

@return tag is used to document the return value of the method.

Syntax
@return description

As per convention, do not use @return tag on void methods and constructors.

Arithmetic.java
package com.java.tags;

/**
 * 
 * @author Hari Krishna
 *
 * @param <T>
 *            Type of element to perform Arithmetic operations
 */
public interface Arithemtic<T extends Number> {

 /**
  * 
  * @param a
  *            First Operand
  * @param b
  *            Second Operand
  * 
  * @return sum of two operands a and b
  * 
  * @since Version 1.1
  * 
  */
 T sum(T a, T b);
}

javadoc is generated like below.




Previous                                                 Next                                                 Home

Javadoc: @param tag

By using @param tag, we can document the description of method parameters.

Syntax
@param parameter-name description

This tag is valid only in a documentation comment for a method, constructor, or class.

The parameter-name can be the name of a parameter in a method or constructor, or the name of a type parameter of a class, method, or constructor. Use angle brackets around this parameter name to specify the use of a type parameter.

Arithmetic.java
package com.java.tags;

/**
 * 
 * @author Hari Krishna
 *
 * @param <T>
 *            Type of element to perform Arithmetic operations
 */
public interface Arithemtic<T extends Number> {

 /**
  * 
  * @param a
  *            First Operand
  * @param b
  *            Second Operand
  * 
  * @return sum of two operands a and b
  * 
  * @since Version 1.1
  * 
  */
 T sum(T a, T b);
}





Previous                                                 Next                                                 Home

Javadoc: @exception: Specify the exception thrown by a method

@exception tag is used to specify the exception thrown by a method (or) constructor.

Syntax
@exception Exception_Class_Name

What is the difference between @throws and @exception tags?
No difference, both are identical (synonyms).

Arithmetic.java
package com.java.tags;

/**
 * 
 * @author Hari Krishna
 * 
 * @since 1.0
 */
public interface Arithmetic {

 /**
  * The value of PI is {@value}
  */
 public static final double PI = 3.14;

 /**
  * 
  * @param radius
  *            radius of the circle
  * 
  * @return area of the circle
  * 
  * @exception IllegalArgumentException
  *                thrown if radius &lt; 0.
  */
 public double areaOfCircle(int radius) throws IllegalArgumentException;

}

When you generate javadoc, it looks like below.




Previous                                                 Next                                                 Home

Javadoc: @deprecated: Document deprecated APIS

@deprecated tag is used to document the deprecated APIS. IDEs like Eclipse visualize the deprecated API by cross line on the api name.

Ex
Before adding @deprecated tag to the method ‘areaOfCircle’ in Eclipse.

After adding @deprecated tag to the method ‘areaOfCircle’ in Eclipse. You can observe a line across the method name.
Arithmetic.java

package com.java.tags;

/**
 * 
 * @author Hari Krishna
 * 
 * @since 1.0
 */
public interface Arithmetic {

 /**
  * The value of PI is {@value}
  */
 public static final double PI = 3.14;

 /**
  *
  * @param radius
  *            radius of the circle
  * 
  * @return area of the circle
  * 
  * @exception IllegalArgumentException
  *                thrown if radius &lt; 0.
  * 
  * @deprecated Do not use this method, use {@link #getAreaOfCircle(int)}
  */
 public double areaOfCircle(int radius) throws IllegalArgumentException;

 /**
  * 
  * @param radius
  *            radius of the circle
  * 
  * @return area of the circle
  */
 public double getAreaOfCircle(int radius);

}

Javadoc is generated like below.



Previous                                                 Next                                                 Home

Javadoc: @author tag

@author tag is used to specify the author of the module.

Syntax
@author author1, author2…author

You can specify multiple author names in one @author tag (or) you can use multiple author tags to specify multiple author names.

Arithmetic.java
package com.java.tags;

/**
 * 
 * @author Hari Krishna
 * @author Keerthi
 * 
 * @since Version 1.1
 *
 */
public interface Arithemtic {

 /**
  * 
  * @param a
  *            First Operand
  * @param b
  *            Second Operand
  * @return sum of two operands a and b
  * 
  * @since Version 1.1
  * 
  */
 int sum(int a, int b);
}


When you generate Javadoc for above interface, it looks like below.

As you above image, author names are part of Author section in the java documentation.

Previous                                                 Next                                                 Home

Javadoc tags

I am going to explain frequently used tags provided by java. Javadoc tool parses these tags, when they are embedded in Javadoc comments.

Tags starts with @ sign.

Ex:

Is tags are case sensitive?
Yes

Tags are categorized into two types
Block tags: Place block tags only in the tag section that follows the description. Block tags have the form: @tag.

Inline tags: Place inline tags anywhere in the main description or in the comments for block tags. Inline tags are enclosed within braces: {@tag}.

Following are the block tags that I used frequently

Following are the inline tags that I used frequently

Reference


Previous                                                 Next                                                 Home