Each line should contain at most one statement. For example,
a = b + c; count++; // WRONG a = b + c; // RIGHT count++; // RIGHT
Generally, local variable declarations should be on separate lines; however, an exception is allowed for temporary variables that do not require initializers. For example,
int i, j = 4, k; // WRONG int i, k; // acceptable int j = 4;
Compound statements are statements that contain a statement block enclosed in {} braces. All compound statements follow the same braces style; namely, the style commonly known as the "K & R" braces style. This includes class and method declarations. This style is specified below.
The opening left brace is at the end of the line beginning the compound statement.
The closing right brace is alone on a line, indented to the same column as the beginning of the compound statement.
The statements inside the enclosed braces are indented one more level than the compound statement.
In cases where the language allows it, the braces may be omitted in the following situations:
The statement block consists of the null statement ";".
The statement block consists of a single simple (not compound) statement that fits on a single line.
The rules on how to format particular statements are described below.
if (condition) { statements; } if (condition) { statements; } else { statements; } if (condition) { statements; } else if (condition) { statements; } else { statements; }
for (initialization; condition; update) { statements; }
while (condition) { statements; }
do { statements; } while (condition);
switch (condition) { case 1: case 2: statements; break; case 3: statements; break; default: statements; break; }
try { statements; } catch (exception-declaration) { statements; } finally { statements; }
synchronized (expression) { statements; }
Labeled statements should always be enclosed in braces {}. The label itself should be indented to the normal indentation level, followed by a colon , single space, and opening brace. The closing brace should have a trailing comment on the same line with the label repeated:
statement-label: { } // statement-label