Eclipse: Changing the Java Code Style Formatting

This tutorial shows you how to set up your Java code style formatting preferences in Eclipse. Specifically, it will show you how to set code styles for tab indents, curly braces, control statements, and code comments.

× Note:   Eclipse also lets you set code styles for parentheses, white space, blank lines, and new lines. You can experiment with those settings on your own.
  1. In Eclipse, choose Window menu > Preferences:

  2. In the Filter text box, type formatter. Eclipse automatically updates the categories in your Preferences window:

  3. Choose Java > Code Style > Formatter (see above).

  4. In the Formatter settings, note the sample code styles shown in the Preview pane, then hit the New... button:

  5. In the New Profile dialog, type a name for your new code formatting profile, then hit OK:

  6. The Formatter window appears. The Preview panel automatically displays any changes you make.

    Which set of code style formats do you want to change?

  7. Hit the Indentation tab to change these settings.

    • Change Tab Size: to set how many spaces Eclipse indents your code when you hit the Tab key. Many developers prefer a tab size of 3 or 4 spaces.

    • In the Indent section, check or uncheck any box to set when Eclipse indents certain kinds of statements (recommended settings in the figure below):

    Hit the Braces tab to change these settings.

    Hit any drop-down box in the Brace Positions section to change how Eclipse places your starting curly braces after a block statement: Same line, Next line, Next line indented, or Next line on wrap.

    Hit the Control Statements tab to change these settings.

    Check ON or OFF any checkbox to control how Eclipse places certain block statements (e.g., else, catch): on the same line as the end of the previous block, or on a new line:

    Hit the Comments tab to change these settings.

    Check on or off any checkbox to control how Eclipse treats line, block, and Javadoc comments (recommended settings in the figure below):

  8. Hit OK when you're finished.

  9. Hit the Apply and Close button to save your changes.

× Note:   To change your code style formatting later, select your code style profile from the Active Profile menu, then hit the Edit... button.