IT Community - Software Programming, Web Development and Technical Support

Java:Reference - Proper Comments

This is a discussion on Java:Reference - Proper Comments within the Java Programming forums, part of the Software Development category; What is a comment? How do you use comments in Java? When should they be used? If you are wondering ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > Java Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 02-23-2007, 09:49 AM
pranky pranky is offline
D-Web Programmer
 
Join Date: Feb 2007
Posts: 51
pranky is on a distinguished road
Default Java:Reference - Proper Comments

What is a comment? How do you use comments in Java? When should they be used? If you are wondering you should read on

Prerequisites
None.

The Idea
Probably the most important piece of code you can write is a comment. Comments not only make your code more clear to others, it will also help you to remember what you were thinking for future development.

The Solution
A comment is a piece of code that is ignored by the compiler that allows others to understand what is going on. In java there are two ways you can comment your code. You can use the traditional double forward slash like this
Code:

//This is a comment

which comments everything to the left of the double forward slashes and ends with a new line. Or the other commonly used comment for commenting multiple is the forward slash asterisk like this:
Code:

/**
using this
You can
Comment multiple
Lines
*/

It is a common standard to comment every method in a class and include the parameters and return values. The java docs make specific use of this. Lets say we are giving a simple class like the one below:
Code:

package helloworld;
public class Hello {
private String name;
public hello(String aName) {
name = aName;
}

public String sayHello(){
Return "Hello, " + name + "!";
}
}

If this code were well commented it would look like this:

Code:

package helloworld;
public class Hello {
private String name;
/**
Constructs a Hello object that can greet a person.
@param aName the name of the person who should be addressed.
*/

public hello(String aName) {
name = aName;
}
/**
Greet with a "Hello" message.
@return a message containing “Hello” and the
name of the person.
*/
public String sayHello(){
Return "Hello, " + name + "!";
}
}

The first line of a comment should be well formed to give the holistic idea of what that method does. In comments parameters are commented using the @param and return values are commented using @return. If you properly comment your documents, you can use Java's javadoc utility to create a series of HTML files that document your classes and methods, but javadocs use your comments!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reference varghese HTML, CSS and Javascript Coding Techniques 0 11-09-2007 05:54 AM
Can we put the reference dll in to a specific folder? amansundar C# Programming 3 10-29-2007 12:51 AM
How to get a reference to primary thread from process ? theone Mobile Software Development 1 07-30-2007 03:50 AM
Difference between // comments, /* */ comments and /// comments vigneshgets C# Programming 1 07-12-2007 04:27 AM
Java:Reference - Operators pranky Java Programming 0 02-24-2007 12:55 AM


All times are GMT -7. The time now is 10:56 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0