Indentation shows which lines of Java belong to a method, loop, or conditional. In Eclipse on the Linux desktop, the active Java formatter profile controls indentation, brace placement, and line wrapping when you format source code.
The workspace profile supplies the default style for Java projects. A project with its own formatter settings uses those settings in place of the workspace default.
A custom profile starts with the rules from an existing profile. You can change individual rules, such as indentation width, and keep the rest of the style you started with.
Steps to configure code formatting in Eclipse:
Start in Eclipse with a Java project that uses workspace formatter settings and a scratch Java file with at least one nested block. The file uses four spaces at the first indent level and eight at the next, as Hello.java does for greeting() and its return statement.
If the Install Java 27 Support notice appears at startup, you can install the support or postpone it with Remind Me Later. Continue at step 1 once your Java project is visible in Project Explorer.
- Open that scratch Java file from Project Explorer (
Hello.javain thehelloproject under~/eclipse-workspacehere).
- Open Window > Preferences….

- Type
formatterinto the Preferences search box.
- Select Java > Code Style > Formatter in the filtered tree.

- Click New… below Active profile.
The profile shown under Active profile, Eclipse [built-in] here, is the one to reselect if you want to restore the previous workspace style.
- Enter
Two Spacesin Profile name.
The Initialize settings with the following profile list shows the base profile. The example uses Eclipse [built-in].
- Select Open the edit dialog now if it is clear.

- Click OK in New Profile to create the profile.

- Click the profile editor's Filter field.

- Type
Tab policyinto Filter.
- Open the Tab policy list.

- Choose Spaces only.

- Click back into Filter.

- Replace the text in Filter with
Indentation size.
- Click the Indentation size value field.

- Replace the Indentation size value with
2.
- Click OK in the profile editor to save the settings.

- Click Apply and Close with
Two Spacesshown as the Active profile.
- Open the Source menu in the Java editor with no text selected.

- Choose Format from the Source menu.
Staging selected changes in Git can keep formatting edits in a separate commit.
Related: How to stage part of a file in Git - Verify in the editor that the same two lines now start two and four spaces in.
In
Hello.java,greeting()starts two spaces in and itsreturnstatement starts four spaces in.