Java Code Formatter

CodeMorph

CodeToHtml

Coding Standards

Java Coding Standard


You can read and download this Java coding standard, coding style guide, code convention, code guideline, coding manual or reference from here for free at your own risk. All trademarks, registered trademarks, product names and company names or logos mentioned herein are the property of their respective owners.

Java Coding Standard

Author: Philip Johnson
Date: January, 1996

Purpose To guide the development of Java Programs, and to ensure that each logical line (as documented in java LOC Counting Standard) corresponds to one physical line.
Program Headers Begin all programs with a descriptive header. The header should use the Java documentation commenting convention ("/**") so that automated documentation generation is possible.
Header example
/**
 * meanStd: calculates the mean and standard deviation of the numbers
 * supplied on the command line.
 * @author         Philip Johnson
 * @version        Tue Dec 26 11:03:18 1995
 */
Version control It is useful, but optional to include automated time/date stamping of creation and modification dates. Emacs and other decent editors will do this for free.
VC Example
//                          -*- Mode: Java-Decaf -*-
// meanStd.java --
// Author          : Philip Johnson
// Created On      : Tue Dec 26 11:02:36 1995
// Last Modified By: Philip Johnson
// Last Modified On: Thu Jan  4 16:02:01 1996
// RCS: $Id$
//
//   Copyright (C) 1995 Philip Johnson
//
//
Identifiers
  • Use descriptive names for all variables, function names, constants, and other identifiers. Use single letter identifiers only for the counter in loops.
  • Follow the "standard Java" style as found in Sun's sources:
    • Identifiers start with lower case.
    • Multi-word identifiers are internally capitalized.
    • Do not use hyphens or underscores to separate multi-word identifiers.
  • Identifier Example
    private static float sumDiffSquares = 0;  //this is good
    private static float x = 0;  //this is bad
    
    Comments
  • Comment the dynamic and static structure of the program.
  • Each class, method, and public variable should have a comment header (in "/**" notation).
  • Blank Spaces Use one blank space to separate classes and methods.
    Indenting Follow the "standard Java" style as found in Sun's sources:
    • Indent two spaces at a time.
    • Open braces (i.e. "{") do not start a new line.
    • Close braces (i.e. "}") do start a new line, and are indented with the code they close.
    Indenting Example
    for (int i=0; i < args.length; i = i + 1) {
      vals.insertElementAt(new Float (args[i]), i);
    }
    
    Capitalization Follow the (regrettable) "standard Java" style as found in Sun's sources:
    • Classes begin with a capital letter.
    • Packages are all lower case.
    • Methods begin with a lower case letter.
    • Multi-word identifiers are internally capitalized in methods.
    Capitalization Examples
    public class MeanStd
    private static Vector vals = new Vector();
    private static Vector diffSquares = new Vector();
    
    Program Modules
  • Each class should be contained in its own file.
  • Each file should be named as the name of the class contained within it.
  • Packages are preferred.
  •   Reformat Java programs by hand?  Try SourceFormatX Java Source Code Formatter to format your java code automatically!