Braces must be used in the if/else blocks, even if there is a single statement. To illustrate:
if (true) {
doThis();
}
The following is not allowed:
if (true)
doThis();
The position of the braces should be the same as in the first example. The following format is incorrect:
if (true)
{
test1();
test2();
}
All exceptions require a statement; no silent catching is allowed. For example:
try {
doThis();
} catch (Exception e) {
// should not occur
}
A logger can be used:
try {
doThis();
} catch (Exception e) {
logger.logDebug("Exception while doing .....", e);
}
Inline conditionals are not allowed. The following code is incorrect:
b = isOk() ? true : false;
The correct way to write this is as follows:
if (isOk()) {
b = true;
} else {
b = false;
}
Declarations are static final, not final static. This is a JLS recommendation.
Constants should be static and final, and should adhere to the following:
'^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'
Constants must be used in the code and magic number must be avoided. For example, the following is not allowed:
private int myAttribute = 5;
The correct format is:
/** * Default value */ private static final int DEFAULT_VALUE = 5; /** * This attribute is initialized with the default value */ private int myAttribute = DEFAULT_VALUE;