53

In Java the following is completely valid:

if (x == null)
    Y();
else
    Z();

I personally don't like it at all. I like all my IF statements to have braces:

if (x == null) {
    Y();
} else {
    Z();
}

The eclipse formatter is wonderful and can beautify my code in many other ways.

Is there a way to have it add the braces to IF statements?

3

5 Answers 5

86

Under "Preferences": Java > Editor > Save Actions

1) Check "Additional actions"

2) Click "Configure…"

3) Go to the "Code Style" tab

4) Check "Use blocks in if/while/for/do statements" and configure to your preferences

4
  • 4
    Which version of eclipse are you using? it doesn't seem to work with me
    – Pacerier
    Commented Oct 19, 2011 at 5:53
  • 1
    I dont know why, but my brain kept forcing me to look for this setting in Java->code style > formatter. This is totally a different section. Thanks :) Commented Dec 30, 2019 at 6:42
  • 1
    This is not a valid solution anymore (obsolete) and Eclipse is not what is used to be 10 years ago. ;) Commented Feb 22, 2021 at 11:58
  • In 2022, with 4.22 (2021-12), in Preferences -> Java -> Editor -> Save Actions, enable Additional Actions and click Configure. Choose the Code Style tab (2nd). Right at the top is Use blocks in if/while/for/do statements; select it and choose the Always button below it. Comments after a naked if go away.
    – Alan
    Commented Jan 20, 2022 at 20:36
22

Yes.

Eclipse menu: Source -> Clean Up...

Configure... -> Code Style -> Use blocks in if/while/for/do statements.

4
  • 2
    I liked this option more than the @gk5885's one... sorry :) A word of caution, Eclipse will delete the comments you had after the if.
    – ArturoTena
    Commented Dec 30, 2014 at 18:43
  • 1
    This option seems to have disappeared from Eclipse Oxygen (2017).
    – simon
    Commented Jul 27, 2017 at 10:53
  • 1
    What is the property name for that to find in formatter xml file ?
    – MAK Ripon
    Commented Oct 21, 2020 at 2:59
  • I really need to know if this 'preference' can be inserted into an Eclipse formatter XML file, as I use NetBeans with an External Formatter and I need to conform to the majority of my colleagues whom are Eclipse addicts...except...this is just one option my OCD cannot tolerate...missing braces!
    – Darrin
    Commented Jun 23, 2023 at 18:10
10

In Eclipse Oxygen (2017+), that option is now at:

Window -> Preferences -> Java -> Code Style -> Clean Up -> Edit -> second tab "Code Style"

After changing, select the corresponding parts of your code and run the "Clean Up" option.

0

This is what I did: Eclipse -Preferences-Java-Code Style-Formatter-Edit-Braces Select next line as desired in each box then save as a different name (your choice).

1
  • This will not add braces to existing code created without braces. Commented Aug 11, 2017 at 21:03
-7

I usually do it by creating a code formatter by copying the built in eclipse formatter, here:

Windows > Preferences > Java > Code Style > Formatter 

Select the profile that you have just created. and click on Edit and go to "Braces" tab.

Change the braces placement as you like (it's a WYSIWYG kind of editor, makes it easy) .

When I need to format, I just select all the text and Ctrl+Shift+F and it works fine for me.

1
  • 3
    This will not add braces to existing code created without braces.
    – yellavon
    Commented Oct 16, 2013 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.