Computer Science Department, Bucknell University

Java Programming Style Guidelines
CSCI 475

Introduction

One of our goals is to write programs that are easily understood by the reader especially the other team members. This becomes particularly important when programs get large. For example, suppose you find a reference to an unfamiliar identifier in a program. You may have to look through many screens of code to find the declaration of that identifier to find out that it is the name of a class. A much better alternative is to use identifier names that tell you something about the name. Ideally, you should be able to look at an identifier and determine whether it is the name of a function, object or class. This document suggests naming guidelines that are based on ones used in industry by companies that produces Java Application Programming Interfaces (APIs) or class libraries. The guidelines are based on the one for C++ in CSCI 204 and Java coding standards at SUN and other companies.

To realize the benefits of having a coding standard for a team, every programmer on the team must abide by it.

Java Source File Organization

A Java source file contains the following sections:

A class definition is preceded by a comment describing the class. Within the class definition, objects (variables) are defined first followed by methods. Objects are defined in the order public, protected then private. Each class object and method is preceded by a comment that describes it.

The file Template.java can be used as a template for creating Java source files. Note that the following uses comments which can be read by SUN's javadoc documentation tool. See Appendix G on page 1271 in Java How to Program by Deitel and Deitel, third edition. The "@" characters are javadoc tags. Documentation comments begin with "/**".

/*
 * File:        Template.java
 * Description: Purpose of the program
 */
import javax.swing.*;

/**
 * Comment describing class
 * @author  Sally Brown
 * @version 0.1
 * Date created: October 15, 2000
 * Date updated: October 20, 2000
 */

public class Template {

  /* Class data members: public, protected then private
   */

  /**
   * mAge is age of employee; mAge > 0
   */
  public int mAge;

  /**
   * Method with no parameters and no return value
   */
  public void Display(){

  }

  /**
   * Method with  parameters and return value
   *
   * @param parameter1 description
   * @param parameter2 description
   * @return value
   */
  public int Add(int inX, int inY) {
     int z;
     z = inX + inY;
     return z;
  }
}
/*
 * End Template.java
 */

Spacing and Comments

Class Names

Any class that you define should be a noun and begin with an uppercase C. This helps you to distinguish them from the ones in a class library. For example, a class defining rational numbers might be called CRational.

Object Names

Object (variable) names begin with a lowercase letter and use embedded uppercase letters for word breaks. Do not use underscores in object names. The first letters of the object name are the prefix, reflecting how the object is used:

   Prefix  Meaning                    Example
     m     data member of a class     mFrameSize
     s     static data member         sClickCount 
     in    input parameter            inCommand 
     out   output parameter           outMenu
     io    input/output parameter     ioParam 
     the   a local copy of an object  thePane 
Any object without the prefix is local.

Function Names

Function names should be verbs and begin with an uppercase letter and use embedded uppercase letters for word breaks. Here are some examples:

   ReadCharacter(); 
   RefreshScreen();
If a function answers a yes/no question and returns a boolean, begin its name with Is. For example, you might define a function called IsDigit() to test if a character is a digit. However, you should call a function (method) that tests for equals equals() since this is a Java idiom.

Constants

Constants should contain an underscore, which is also a word break. The under-score is never the first or last character. Embedded capitals are used for other word breaks.

   final int Bow_Wow = 1;


Page maintained by hyde@bucknell.edu Last update October 23, 2000

Back to Computer Science Department's Home page.