Use javadoc to standardize Java Development documents

Source: Internet
Author: User

Usually we write Java programs that may rarely write comments, but when developing projects in the company. There are usually strict document requirements. I am not talking about design or test documents, but javadoc. I always think that javadoc is easier to view than msdn, and it is not complex to write.

Javadoc is a very important tool in j2sdk. If you write comments in the Java source code according to the specifications, it can generate corresponding documents. It is very convenient for developers to check. Many ides can generate javadoc directly. This section describes how to write Javadoc and how to generate javadoc in eclipse.

Javadoc usually extracts information from package, public class or interface, public or protected fields, public or protected methods. Each comment should start with/** and end. For example
/**
*
* @ Param ID the coreid of the person
* @ Param username the name of the person

* You shoshould use the constructor to create a person object
*/
Public secondclass (int id, string username)
{
This. ID = ID;
This. Username = username;
}
The comment should be written before the description, as shown above. And can include HTML tags.
Then you shoshould USR the... will keep @ Param username... In javadoc, which is not what we want. Comments can be classified into class comments, method comments, and field comments. The following is a brief introduction.

  1. Class Annotation
    Class annotation should be followed by the class declaration after the Import Statement, for example
    Package com. North. Java;

/**
* @ Author Ming
*
* This interface is to define a method print ()

* You shoshould implements this interface is you want to print the username

* @ See COM. North. Ming. mainclass # Main (string [])
*/
Public interface dosomething
{
/**
* @ Param name which will be printed
* @ Return nothing will be returned
*
*/
Public void print (string name );
}
Both @ author and @ see are commonly used annotations. The first annotation represents the author and the second annotation represents the reference connection.

2.Method comment
The method annotation should be followed by the method. You can use tags such as @ Param @ return @ throws in the annotation. For example
/**
*
* @ Param I
* @ Return true if... else false
* @ Throws ioexception when reading the file, if something wrong happened
* Then the method will throws a ioexception
*/
Public Boolean domethod (int I) throws ioexception
{
Return true;
}

3.Field comment
Only public fields need to be annotated, usually static, such
/**
* The static filed hello
*/
Public static int Hello = 1;

In eclipse, we create a Java project and write several interfaces and classes. Then we can use javadoc to generate documents. Select generate javadoc from the menu project, and a wizard will appear, you follow his prompt to set the requirements step by step. It is best for him to ask if you claim a javadoc. XML. If you select generate, it will generate a javadoc under the doc. XML. You can use ant to run javadoc directly when updating documents. XML. After the selection, you can find that a directory doc in the project contains your javadoc. A very good way to write a good javadoc is to refer to the Java API Doc. It is very important to develop a good programming habit. Below is my Blog Code and comments
/*
* Created on 2004-7-25
*
* Todo to change the template for this generated file go
* Window-preferences-Java-code style-code templates
*/
Package com. North. Ming;

/**
* @ Author p2800
*
* Todo to change the template for this generated type comment go
* Window-preferences-Java-code style-code templates
*/
Public class mainclass
{

Public static void main (string [] ARGs)
{
}
}
/*
* Created on 2004-7-25
*
* Todo to change the template for this generated file go
* Window-preferences-Java-code style-code templates
*/
Package com. North. Ming;

Import java. Io. ioexception;

/**
* @ Author p2800
*
* Todo to change the template for this generated type comment go
* Window-preferences-Java-code style-code templates
*/
Public class secondclass
{
 
/**
* The static filed hello
*/
Public static int Hello = 1;
Private int ID;
Private string username;

/**
*
* @ Param ID the coreid of the person
* @ Param username the name of the person

* You shoshould use the constructor to create a person object
*/
Public secondclass (int id, string username)
{
This. ID = ID;
This. Username = username;
}

/**
* @ Return returns the username.
*/
Public String GetUserName ()
{
Return username;
}
/**
* @ Param username the username to set.
*/
Public void setusername (string username)
{
This. Username = username;
}

/**
*
* @ Param I
* @ Return true if... else false
* @ Throws ioexception when reading the file, if something wrong happened
* Then the method will throws a ioexception
*/
Public Boolean domethod (int I) throws ioexception
{
Return true;
}

}
/*
* Created on 2004-7-25
*
* Todo to change the template for this generated file go
* Window-preferences-Java-code style-code templates
*/
Package com. North. Java;

/**
* @ Author Ming
*
* This interface is to define a method print ()

* You shoshould implements this interface is you want to print the username

* @ See COM. North. Ming. mainclass # Main (string [])
*/
Public interface dosomething
{
/**
* @ Param name which will be printed
* @ Return nothing will be returned
*
*/
Public void print (string name );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.