|
http://www.jindent.com |
Previous: Method/Constructor
|
Next: Enum
|
| Brace style for general Java code (statements, arrays, initializers...) |

| Left brace { new line |
|
while (a < 10) { ... |
|
while (a < 10) { ... |
| Right brace } new line |
if-else or do-while statements appears in a new line
or not.|
if (a < 10) { ... } else { ... } do { ... } while (a < 10); |
|
if (a < 10) { ... } else { ... } do { ... } while (a < 10); |
| Indent left brace { |
|
while·(a·<·10) ····{ ····... |
|
while·(a·<·10) { ... |
|
while·(a·<·10)·{ ... |
|
while·(a·<·10){ ... |
| Indent right brace } |
|
while·(a·<·10) ····{ ····... ····} |
|
while·(a·<·10) { ····... } |
| Indent after right brace } |
if-else and do-while statements.|
if (a < 10) { ... }else { ... } do { ... }while (a < 10); |
|
if (a < 10) { ... } else { ... } do { ... } while (a < 10); |
| Cuddle braces of empty blocks {} |
|
while (condition) {} |
|
while (condition) { } |
| Indent cuddled braces {} |
|
while·(condition){} |
|
while·(condition)·{} |
| Prohibit blank lines after left brace { |
|
while (a < 10) { ¶ // comment with a preceding blank line callAnotherMethod(); ¶ // another comment ... } |
|
while (a < 10) { // comment with a preceding blank line callAnotherMethod(); ¶ // another comment ... } |
|
See also: | Jindent - Settings - Formatter - Java / SQLJ - Comments - Blank Lines , Jindent - Settings - Formatter - Java / SQLJ - Blank Lines |
| If number of lines in body is at least ... then insert blank line after { |
|
while (a < 10) { callMethod(--a); } ¶ for (int i = 0; i < 10; i++) { callMethodA(i); for (int i = 0; i < n; i++) { callMethodB(i); callMethodC(i); } callMethodD(i); } ¶ if (x == 0) { callMethodA(); callMethodB(); } else { callMethodC(); callMethodD(); } |
while(a < 10) looks fine,
but statement for (int i = 0; ...) { seems to be formatted too close:
The statement head for (int i = 0; i < 10; i++) { and the first block statement
callMethodA(i); are connected too close and could cause confusion.
|
while (a < 10) { callMethod(--a); } ¶ for (int i = 0; i < 10; i++) { ¶ callMethodA(i); for (int i = 0; i < n; i++) { ¶ callMethodB(i); callMethodC(i); } callMethodD(i); } ¶ if (x == 0) { ¶ callMethodA(); callMethodB(); } else { ¶ callMethodC(); callMethodD(); } |
for (int i = 0; ...) { seems to be much clearer and
statement while (a < 10) { is still formatted as before.|
while (a < 10) { callMethod(--a); } ¶ for (int i = 0; i < 10; i++) { ¶ callMethodA(i); for (int i = 0; i < n; i++) { callMethodB(i); callMethodC(i); } callMethodD(i); } ¶ if (x == 0) { callMethodA(); callMethodB(); } else { callMethodC(); callMethodD(); } |
if (x == 0) { is formatted in the same way as while (a < 10) {.|
while (a < 10) { ¶ callMethod(--a); } ¶ for (int i = 0; i < 10; i++) { ¶ callMethodA(i); for (int i = 0; i < n; i++) { ¶ callMethodB(i); callMethodC(i); } callMethodD(i); } ¶ if (x == 0) { ¶ callMethodA(); callMethodB(); } else { ¶ callMethodC(); callMethodD(); } |
infinite.| If number of lines in body is at least ... then insert blank line before } |
|
while (a < 10) { callMethod(--a); } ¶ for (int i = 0; i < 10; i++) { ¶ callMethodA(i); for (int i = 0; i < n; i++) { ¶ callMethodB(i); callMethodC(i); ¶ } callMethodD(i); ¶ } ¶ if (x == 0) { ¶ callMethodA(); callMethodB(); ¶ } else { ¶ callMethodC(); callMethodD(); ¶ } |
| Do not insert blank line before single left brace |
|
while (a < 10) { callMethod(--a); } // <- this right brace appears alone ¶ if (a < 10) { callMethodA(); callMethodB(); } else { // <- this right brace does not appear alone callMethodC(); callMethodC(); } // <- this right brace appears alone |
} else { does not appear alone,
it is directly followed by an else token.|
while (a < 10) { callMethod(--a); } ¶ for (int i = 0; i < 10; i++) { ¶ callMethodA(i); for (int i = 0; i < n; i++) { ¶ callMethodB(i); callMethodC(i); ¶ } callMethodD(i); ¶ } ¶ if (x == 0) { ¶ callMethodA(); callMethodB(); ¶ } else { ¶ callMethodC(); callMethodD(); ¶ } |
if-else statements, but for all other statements
the extra blank lines before the right braces seems to be unnecessary.|
while (a < 10) { callMethod(--a); } ¶ for (int i = 0; i < 10; i++) { ¶ callMethodA(i); for (int i = 0; i < n; i++) { ¶ callMethodB(i); callMethodC(i); } callMethodD(i); } ¶ if (x == 0) { ¶ callMethodA(); callMethodB(); ¶ } else { ¶ callMethodC(); callMethodD(); } |
} else { because this is the only case a
right brace does not appear as a single brace.
|
See also: | Jindent - Settings - Formatter - Java / SQLJ - Braces Style - Presets , Space before left braces of array initializers |