b2KIT

Java Formatter

Format Java source code with configurable brace style, indentation, and import ordering.

Tested tool guide Tested browser tools Checked August 16, 2026

What Java Formatter does and how it behaves

Java Formatter turns pasted Java source into a layout governed by the chosen brace style, indentation, and import-order settings. Its output targets Java-specific structure: package and import declarations, type and method bodies, and nested statement blocks. Use it to make inconsistent source conform to a selected presentation before review or comparison. The main surprise is that the Java Language Specification defines legal syntax, not one compulsory house style. Matching this result in an IDE therefore requires matching the formatter's options, not merely selecting Java as the language.

How the result is produced

1

Brace placement and nesting

The brace-style setting determines placement for block braces in Java declarations and control-flow statements. The indentation setting determines the offset used as class, method, and statement blocks nest. Together they turn mixed layouts into one selected layout, so an if block inside a loop is indented relative to both enclosing levels. These controls govern presentation and do not establish whether the underlying statements are legal Java.

2

Import declaration ordering

Java compilation units reserve a region for import declarations after an optional package declaration and before top-level type declarations. The import-order setting reorganizes that region according to the selected preference, making import lists compare consistently. Treat this as source organization, not name resolution: visual order cannot show whether a referenced class exists, whether an import is unused, or whether a wildcard import is appropriate.

Good uses

  • Normalize brace placement and nested indentation in a Java file copied from a tutorial or produced by a source generator before conducting a focused code review.
  • Reorder imports after several manual edits so a class's declaration section follows the same selected ordering rule as neighboring Java files.
  • Apply a proposed Java house style to a representative source sample before configuring an IDE or repository formatter to produce matching layout.

Limits and checks

  • The result is tied to the exact settings. Another brace choice, indentation width, or import rule can produce different text from identical Java source.
  • Formatted output is not a compiler verdict. Syntax errors, unresolved types, inaccessible members, incorrect generic arguments, and runtime behavior require separate Java tooling.
  • Import order is a convention, not dependency order. Earlier imports are not initialized first, and a tidy import block does not prove every declaration is required.

Common questions

Is this the official Java formatting style?

No. The Java Language Specification describes valid compilation units and statements but does not mandate one brace placement, indentation width, or project-wide import sequence. This formatter applies the options you select. It matches a team standard only when those selections match that team's documented profile; Google style, an IDE default, and another project's conventions may differ.

Will formatting fix imports or compilation errors?

No, not as a substitute for compilation or IDE analysis. Import ordering can rearrange declarations under the selected policy, but ordering alone cannot determine whether an import is missing, unused, ambiguous, or available on the class path. Likewise, indentation cannot repair a wrong type, method call, or control-flow condition. Compile the returned source and inspect its diff before adopting it.

References and verification

The behavioral notes were checked against the browser implementation. Standards and primary references below define the relevant format, formula, or platform behavior.

Related Tools