Identifiers for classes, interfaces, and enumerations should use "upper camel-case" style. These identifiers consist of words that are jammed together, each word beginning with an upper-case letter. Examples of correct upper camel-case are:
Identifiers for packages and most variables should use "lower camel-case" style. This style is the same as "upper camel-case" except that the identifier begins with a lower-case letter. Examples of correct lower camel-case are:
For most literal values appearing in your code, it is best to used "named constants". For example, rather than this:
we prefer to declare a variable like this:
and then write the statement as follows:
Note that the style for variables serving as "named constants" utilizes all capital letters. Words should be separated by the underscore character. Examples of the correct style for named constants are:
Identifiers should almost always consist of English words that describe the role of the entity in the program. You should avoid identifiers like the following bad examples:
What in the world do those identifiers represent? Who would know?! Identifiers should primarily consist of English words jammed together. That doesn't mean you can never use abbreviations. For example, any of the following identifers seem OK to me:
That last one might be a stretch, but I think it's clear enough.
There are cases where one might correctly use a variable consisting of just one letter, but they are rare. When using a conventional for-loop to iterate over a collection, it is common to see the variable i as an abbreviation for index:
However, in most cases this is not the best style, since a for-each loop is more readable and supresses the need for an index variable. In cases where an index variable is necessary, there is often a better choice for the identifier (e.g. "row", or "col"). We will accept the variable "i" as an identifier only in cases where a for-each loop is not sufficient, and there really is no better choice from context.
There are cases (beyond looping) where a single-letter variable's role is clear from context. For example, when implementing graphical applications, it is common to use the variables x and y to specify coordinates.
We are very fussy about this! Below are examples of correctly placed braces and indentation for many different types of Java statements. You should not be using any other style.
if (.....) {
....
....
}
if (.....) {
....
....
} else {
....
....
....
}
if (.....) {
....
....
} else if (.....) {
....
....
} else if (.....) {
....
....
}
if (.....) {
....
....
} else if (.....) {
....
....
} else {
....
....
}
for(int i = 0; i < a.length; i++) {
....
....
}
while(.....) {
....
....
}
do {
....
.....
} while(.....);
switch(x) {
case 7:
....
....
case 28:
....
....
default:
....
....
}
if (.....)
{
....
....
}
if (x > 10) {
System.out.println("x is large");
}
Don't be lazy and leave off the braces. Even though the following code fragment is functionally equivalent to the above,
it is error-prone and considered bad style:
if (x > 10)
System.out.println("x is large");