About the Java Beautifier
The thing that ruins Java when it goes through a generic code beautifier is generics. A tool built for JavaScript sees List<String> as a comparison and pads it into List < String >, which is ugly at best and confusing at worst. This beautifier never inserts a space between two tokens that were adjacent, so a parameterised type, a diamond operator and a bounded wildcard all survive untouched.
Layout is derived from brace depth. Class bodies, method bodies, initialiser blocks, anonymous inner classes and lambda blocks each add a level. Labels in a switch sit one level inside the switch keyword. A closing brace followed by else, catch or finally keeps its partner on the same line rather than orphaning the keyword.
Annotations keep the line they were written on. If you paste a file where every annotation shares a line with its method, they stay that way, because moving them would be a style decision rather than an indentation fix. Javadoc and block comments are reprinted with their asterisks aligned under the opening marker.
Four spaces is preselected since that is what the Oracle conventions and Google Java Style both use. If what you actually want is a Java class generated from a JSON payload, use JSON to Java instead of formatting one by hand.
How to use
- Paste a
.javafile into the left pane, or press Sample for a small class using a generic list. - Keep Indent at 4 spaces for standard Java style, or pick tabs if your project uses them.
- Switch Split statements onto new lines off when the file already has sensible line breaks.
- Confirm the generics are unchanged, then Copy or Download the class.
Common questions
- Are text blocks written with three double quotes safe?
- Their content is copied verbatim, so the indentation inside a text block is not adjusted. Note that Java itself strips incidental leading whitespace at compile time, so do not rely on this tool to change it.
- Does it move an opening brace to the next line?
- No. Java convention keeps the brace on the same line and the tool never relocates one, so whichever style the file uses is preserved.
- Can it format an interface or an enum?
- Yes. Any file made of brace delimited blocks indents correctly, including enums with constant bodies and interfaces with default methods.
- Will it fix a missing brace?
- No, and it will not silently invent one. The indentation will keep sliding to the right past the point where the brace is missing, which usually makes the spot easy to find.