Java Conventions
1 Java Naming Conventions
1.1 Classes
Class names should be nouns, in mixed case with the first letter of each in-
ternal word capitalized. Try to keep your class names simple and descriptive.
Use whole words—avoid acronyms and abbreviations (unless the abbrevia-
tion is much more widely used than the long form, such as URL or HTML).
Example:
class Point
class ScreenMonitor
1.2 Interfaces
Interface names should be capitalized like class names. Example:
interface Printable
interface Storing
1.3 Methods
Methods should be verbs, in mixed case with the first letter lowercase, with
the first letter of each internal word capitalized.
Variable names should be short yet meaningful. The choice of a variable
name should be mnemonic— that is, designed to indicate to the casual ob-
server the intent of its use. One-character variable names should be avoided
except for temporary “throwaway” variables. Common names for temporary
variables are i, j, k, m, and n for integers; c, d, and e for characters Example:
int i;
char myWidth;
1
1.4 Constants
The names of variables declared class constants and of constants should be
all uppercase with words separated by underscores (“_”). Example:
int MIN_WIDTH = 4;
double PI = 3.14;
2 Blank Spaces
• Blank spaces should be used in the following circumstances: A keyword
followed by a parenthesis should be separated by a space. Example:
while (true) {
...
}
• Note that a blank space should not be used between a method name
and its opening parenthesis. This helps to distinguish keywords from
method calls.
• A blank space should appear after commas in argument lists
• All binary operators except . should be separated from their operands
by spaces. Blank spaces should never separate unary operators such
as unary minus, increment (“++”), and decrement (“–”) from their
operands.
Example:
a += c + d;
a = (a + b) / (c * d);
while (d++ = s++) {
n++;
}
[Link]("size is " + foo + "\n");
• The expressions in a for statement should be separated by blank spaces.
Example:
for (expr1; expr2; expr3)
2
• Casts should be followed by a blank. Example:
myMethod((byte) aNum, (Object) x);
myFunc((int) (cp + 5), ((int) (i + 3)) + 1);
3 Blank Lines
Blank lines improve readability by setting off sections of code that are log-
ically related. Two blank lines should always be used in the following cir-
cumstances:
• Between sections of a source file
• Between class and interface definitions
One blank line should always be used in the following circumstances:
• Between methods
• Between the local variables in a method and its first statement
• Between logical sections inside a method to improve readability
4 Comments
Programs can have four styles of implementation comments: block, single-
line, trailing and end-of-line.
4.1 Block Comments
Block comments are used to provide descriptions of files, methods, data
structures and algorithms. Block comments should be used at the beginning
of each file and before each method. Example:
/*
* Here is a block comment.
*/
3
4.2 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 Example:
if (condition) {
/* Handle the condition. */
...
}
4.3 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.
Example:
if (a == 2) {
return TRUE; /* special case */
} else {
return isprime(a); /* works only for odd a */
}
4.4 End-Of-Line Comments
The // comment delimiter begins a comment that continues to the newline.
It 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.
4
// ...
//} else
// return false;
5 Declarations
One declaration per line is recommended since it encourages commenting.
In other words,
int level; // indentation level
int size; // size of table
is preferred over
int level, size;
6 Placement
Put declarations only at the beginning of blocks. (A block is any code
surrounded by curly braces “{” and “}”.) Don’t wait to declare variables
until their first use; it can confuse the unwary programmer and hamper code
portability within the scope.
void MyMethod() {
int int1; // beginning of method block
if (condition) {
int int2; // beginning of "if" block
...
}
}
The one exception to the rule is indexes of for loops, which in Java can be
declared in the for statement:
for (int i = 0; i < maxLoops; i++) { ...
7 Initialization
Try to initialize local variables where they’re declared. The only reason not
to initialize a variable where it’s declared is if the initial value depends on
some computation occurring first.
5
8 Class and Interface Declarations
When coding Java classes and interfaces, the following formatting rules
should be followed:
• No space between a method name and the parenthesis “(“ starting its
parameter list
• Open brace “{” appears at the end of the same line as the declaration
statement
• Closing brace “}” starts a line by itself indented to match its corre-
sponding opening statement, except when it is a null statement the “}”
should appear immediately after the “{“
class Sample extends Object {
int ivar1;
int ivar2;
Sample(int i, int j) {
ivar1 = i;
ivar2 = j;
}
int emptyMethod() {}
...
}
• Methods are separated by a blank line
You can access the complete Java language coding standards presented in
the Java Language Specification, from Sun Microsystems at [Link]
[Link]/technetwork/java/[Link]