Compact braces

Allows you to print statement blocks in just one line when possible, i.e. when the block only contains one statement and does not exceed the maximal line length. Please note that white space before a compacted block is not controlled by your general brace settings, but with the "Space before braces" option.

Methods

When enabled, method and constructor bodies will be printed in one line when possible.

Since 1.2

Example 2.72. Standard method declaration

public int getMagicNumber() {                                |
    return 23;                                               |
}                                                            |

Example 2.73. Compact method declaration

public int getMagicNumber() { return 23; }                   |

Single if

When enabled, single if statement bodies will be printed in one line when possible.

Since 1.2

Example 2.74. Standard if block

if (cond) {                              |
    return getMagicNumber();             |
}                                        |

Example 2.75. Compact if block

if (cond) { return getMagicNumber(); }   |

if

When enabled, statement bodies of the if the part of if/else statement will be printed in one line when possible.

Since 1.2

Example 2.76. Standard if/else block

if (cond) {                              |
    return getMagicNumber();             |
} else {                                 |
    return -1                            |
}

Example 2.77. Compact if of if/else block

if (cond) { return getMagicNumber(); }   |
else {                                   |
    return -1;                           |
}                                        |

Please note that when the if block has been compacted, the following else if or else statement always starts on a new line.

else if

When enabled, prints else if statement bodies of if/else statements in one line when possible.

Example 2.78. Standard else of if/else block

if (cond1) {                                               |
    return getMagicNumber();                               |
} else if (cond2) {                                        |
    throw new IllegalStateException();                     |
}                                                          |

Example 2.79. Compact else if block

if (cond1) {                                               |
    return getMagicNumber();                               |
} else if (cond2) { throw new IllegalStateException(); }   |

Please note that when the else if block has been compacted, the following else statement always starts on a new line.

Since 1.2

Example 2.80. Compact if block

if (cond1) {                                               |
    return getMagicNumber();                               |
} else if (cond2) { throw new IllegalStateException(); }   |
else {                                                     |
    return 0;                                              |
}                                                          |

else

When enabled, prints else statement bodies in one line when possible.

Since 1.2

Example 2.81. Standard else block

if (cond1) {                                               |
    return getMagicNumber();                               |
} else if (cond2) {                                        |
    throw new IllegalStateException();                     |
} else {                                                   |
    return 0                                               |
}                                                          |

Example 2.82. Compact else block

if (cond1) {                                               |
    return getMagicNumber();                               |
} else if (cond2) {                                        |
    throw new IllegalStateException();                     |
} else { return 0; }                                       |

Arrays

When enabled, array initializers will be printed in one line when possible.

Since 1.7

Example 2.83. Array initializer

String[] s = {                            |
        "First"                           |
    };                                    |

Example 2.84. Compact array initializer

String[] s = { "First" };                 |

Enums

When enabled, enum declaration bodies will be printed in one line when possible.

Since 1.4

Example 2.85. Enum declaration

public enum Mode {                                         |
    OPEN, CLOSE                                            |
}                                                          |

Example 2.86. Compact enum declaration

public enum Mode { OPEN, CLOSE }                           |

Enum constants

When enabled, enum constants will be printed in one line when possible. This option is only useful if you use constant-specific methods in your enums.

Since 1.4

Example 2.87. Enum constants with constant specific methods

public enum Operation {                                           |
    PLUS {                                                        |
        double eval(double x, double y) {                         |
            return x + y;                                         |
        }                                                         |
    },                                                            |
    MINUS {                                                       |
        double eval(double x, double y) {                         |
            return x - y; }                                       |
    },                                                            |
    TIMES {                                                       |
        double eval(double x, double y) {                         |
            return x * y;                                         |
        }                                                         |
    },                                                            |
                                                                  |
    // Do arithmetic op represented by this constant              |
    abstract double eval(double x, double y);                     |
}                                                                 |

Example 2.88. Compact enum constants with constant specific methods

public enum Operation {                                           |
    PLUS { double eval(double x, double y) { return x + y; } },   |
    MINUS { double eval(double x, double y) { return x - y; } },  |
    TIMES { double eval(double x, double y) { return x * y; } },  |
                                                                  |
    // Do arithmetic op represented by this constant              |
    abstract double eval(double x, double y);                     |
}                                                                 |

Enable for

You can narrow the scope for the above mentioned options by selecting whether all statements should be printed on one line when possible or only throw and return statements.

Since 1.2

All statements

When enabled, all statements are compacted when possible.

Example 2.89. All statements on same line

if (true) { return result; }
else if (false) { System.out.println("unexpected condition"); }
else { throw new Error(); }

Only throw and return

When enabled, only throw and return statements are compacted when possible. All other statements are printed in normal block style.

Example 2.90. Only throw and return statements on same line

if (true) { return result; }
else if (false) {
    System.out.println("unexpected condition");
} else { throw new Error(); }