Formatting Java Code: The Conventions

Formatting Java Code: The Conventions

Java is one of the most convention-heavy languages there is — its style guide predates most others and is enforced by every serious team. The rules below are the ones reviewers notice.

The conventions that matter

  • Classes PascalCase, methods and variables camelCase, constants UPPER_SNAKE_CASE — Java’s naming trinity.
  • Braces on the same line (K&R style): if (x) { — not on a new line. The one style choice where Java deviates from C#.
  • 4-space indentation, no tabs.
  • One statement per line; blank lines separate logical blocks.
  • @Override on overridden methods — the compiler enforces it; omitting it is a red flag in review.

Why Java formatting matters more

Java code lives long — systems run for decades, and code passes through many hands. Consistent style is how a 20-year-old file still reads like it was written yesterday. It’s also how tools like static analyzers and diff reviews stay usable: uniform code produces clean diffs.

Formatting

  1. Paste your code into the Java formatter.
  2. The tool applies the standard conventions — indentation, brace placement, spacing.
  3. Copy the result. For teams, adopt an automated formatter in CI so style disputes never reach review.

The honest note

Java’s verbosity is part of its design — explicit types, explicit braces, explicit everything. Formatted Java still looks like Java; it just reads smoothly. The goal isn’t fewer characters, it’s fewer surprises for the next maintainer.

A style guide only works when it’s automatic. Formatters turn “style” from a review argument into a build step — which is where it belongs.