Comments

  • Block Comments

Block comments are used to provide descriptions of files, methods, data structures and algorithms. Block comments may be used at the beginning of each file and before each method. They can also be used in other places, such as within methods. Block comments inside a function or method should be indented to the same level as the code they describe.

A block comment should be preceded by a blank line to set it apart from the rest of the code.


    /*
     * Here is a block comment.
     */
  • Single-Line Comments

Short comments can appear on a single line indented to the level of the code that follows. If a comment can’t be written in a single line, it should follow the block comment format. A single-line comment should be preceded by a blank line. Here’s an example of a single-line comment in Java code.


    if (condition) {

        /* Handle the condition. */
        ...
    }
  • Trailing Comments

Very short comments can appear on the same line as the code they describe, but should be shifted far enough to separate them from the statements. If more than one short comment appears in a chunk of code, they should all be indented to the same tab setting.

Here’s an example of a trailing comment in Java code:


    if (a == 2) {
        return TRUE;            /* special case */
    } else {
        return isPrime(a);      /* works only for odd a */
    }
  • End-Of-Line Comments

The // comment delimiter can comment out a complete line or only a partial line. It shouldn’t be used on consecutive multiple lines for text comments; however, it can be used in consecutive multiple lines for commenting out sections of code. Examples of all three styles follow:


    if (foo > 1) {

        // Do a double-flip.
        ...
    }
    else {
        return false;          // Explain why here.
    }
    //if (bar > 1) {
    //
    //    // Do a triple-flip.
    //    ...
    //}
    //else {
    //    return false;
    //}

Original Post

http://www.oracle.com/technetwork/java/codeconventions-141999.html

Advertisements

Comment Rules For Java Source Files

Each Java source file contains a single public class or interface. When private classes and interfaces are associated with a public class, you can put them in the same source file as the public class. The public class should be the first class or interface in the file.

Java source files have the following ordering:

  • Beginning comments
  • Package and Import statements
  • Class and interface declarations

Java Source File Example

The following example shows how to format a Java source file containing a single public class. Interfaces are formatted similarly.

/*
 * @(#)Blah.java        1.82 99/03/18
 *
 * Copyright (c) 1994-1999 Sun Microsystems, Inc.
 * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of Sun
 * Microsystems, Inc. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Sun.
 */

package java.blah;

import java.blah.blahdy.BlahBlah;

/**
 *
 *
 Class description goes here.
 *
 * @version
 *
 1.82 18 Mar 1999
 * @author
 *
 Firstname Lastname
 */
public class Blah extends SomeClass {

    /* A class implementation comment can go here. */

    /**
     *
     classVar1 documentation comment */
    public static int classVar1;

    /**
     *
     *
     *
     *
     classVar2 documentation comment that happens to be
     *
     *
     more than one line long
     */
    private static Object classVar2;

    /**
     *
     instanceVar1 documentation comment */
    public Object instanceVar1;

    /**
     *
     instanceVar2 documentation comment */
    protected int instanceVar2;

    /**
     *
     instanceVar3 documentation comment */
    private Object[] instanceVar3;

    /**
     * ...
     *
     constructor Blah documentation comment...
     */
    public Blah() {

        // ...implementation goes here...
    }

    /**
     * ...
     *
     method doSomething documentation comment...
     */
    public void doSomething() {

        // ...implementation goes here...
    }

    /**
     * ...method doSomethingElse
     *
     documentation comment...
     * @param someParam
     *
     description
     */
    public void doSomethingElse(Object someParam) {

        // ...implementation goes here...
    }
}

Original Post
http://www.oracle.com/technetwork/java/codeconventions-137946.html#182

Class

A class defines Characteristics of an object at design time. It is a blueprint of an object.

Attributes
• Fields or Properties or variables
Behaviors
• Operations or Methods or functions
For example properties of a car class are
• Color
• Number of doors
• Model
Methods of a car class are
• on()
• off()
• turn()


public class Car{

    public String color;
    public int numberOfDoors;    // these are the properties
    public int model;

    public void on(){
    }

    public void off(){          // these are the methods
    }

    public void turn(){
    }
}

Naming Convention For Class

Class names should be nouns, in mixed case with the first letter of each internal word capitalized.

For example

JavaMahbub, HabaMahbub, Mahbub  etc

 

Naming Convention For Properties

Internal words start with capital letters. Variable names should not start with underscore _ or dollar sign $ characters, even though both are allowed.

For example

int i, int j, String kiHolo etc

 

Naming Convention For Methods

Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized.

For example

go(), jojo(), goJojo() etc

 

 

Want to know more ?

CamelCase (camel case or camel-case), also known as medial capitals, is the practice of writing compound words or phrases in which the elements are joined without spaces, with each element’s initial letter capitalized within the compound and the first letter either upper or lower case—as in “LaBelle”, BackColor, “McDonald’s”, or “iPod”. The name comes from the uppercase “bumps” in the middle of the compound word, suggestive of the humps of a camel. The practice is known by many other names.

Original Post

http://en.wikipedia.org/wiki/CamelCase