Controls miscellaneous brace options.
Per definition braces are superfluous on single statements, but it is a common recommendation that braces should be always used in such cases. With this option, you can specify whether missing braces for single statements should be inserted for the control statements if, for, while and do-while and labeled statements inside switch blocks.
When enabled, braces are inserted around the body of if...else statements when necessary.
Example 2.49. Brace insertion for if statements
if (true) break; else return;
would become
if (true) { break; } else { return; }
When enabled, braces are inserted around the body of for statements when necessary.
Example 2.50. Brace insertion of for statements
for (int i = 0; i < count; i++) System.out.println(i);
would become
for (int i = 0; i < count; i++) { System.out.println(i); }
When enabled, braces are inserted around the body of while statements when necessary.
Example 2.51. Brace insertion for while statements
while (!isDone)
doSomething();
would become
while (!isDone) {
doSomething();
}
When enabled, braces are inserted around the body of do...while statements when necessary.
Example 2.52. Brace insertion for do...while statements
do something(); while (condition);
would become
do { something(); } while (condition);
When enabled, braces are inserted around the body of labeled statements inside switch blocks when necessary.
Please note that braces are only inserted if the statement is not empty!
Since 1.4
Example 2.53. Brace insertion for labeled statements
switch (c) { case 'a': case 'b': System.out.println(); break; }
would become
switch (c) { case 'a': case 'b': { System.out.println(); break; } }
When enabled, brace inseration only happens when the block statement takes more than just one line to print.
Since 1.8
Take the following example with two block statements:
Example 2.54. Missing braces
if (arg == null) for (int i = 0; i < 10; i++) System.out.println("arg " + i);
Enabling brace insertion for if and for statements would normally yield:
Example 2.55. Inserted braces
if (arg == null) { for (int i = 0; i < 10; i++) { System.out.println("arg " + i); } }
You see braces inserted for both block statements.
But when you enabled the multi-line option, you will get:
Example 2.56. Inserted braces limited to multi-line statements
if (arg == null) { for (int i = 0; i < 10; i++) System.out.println("arg " + i); }
The statement of the if-block happens to be another block statement which is printed in two lines here, therefore the braces are inserted for the if-statement. The for-statement on the other hand does not have any braces inserted, because here the block can be printed in just one line.