Style
This style is influenced from the Google Java Style Guide.
Indentation and Spacing / Horizontal Spacing
Always use 2 or 4 space tabbing (i.e. not \t character).
Keep spacing around operators.
int sum = a + b; // goodint sum=a+b; // badLimit line length to be < 100 characters.
Vertical Spacing
Use blank lines to separate logical blocks.
void foo() { validateUserInput(); processAuthentication(); initialize(); otherThing();}Prefer to use vertical separators sparingly, having single lines of code isolated vertically is not preferred.
One blank line between class members (fields, constructors, methods). Two blank lines between top-level class definitions.
Braces
Start braces on the same line.
if (true) { // good}if (true){ // bad}Always use braces, even for single-line bodies.
if (condition) { return; // good}if (condition) return; // badEmpty blocks should be written on one line.
void doNothing() {} // goodvoid doNothing() {} // bad (unnecessarily spread out)Declarations
One declaration per line
// goodint a;int b; // badint a, b;Access modifiers
Always declare access modifiers explicitly.
Order them as: public / protected / private → abstract → static → final → transient → volatile
private static final int MAX = 100; // goodstatic private final int MAX = 100; // badVariable declarations
Declare variables close to where they are first used, not at the top of the block.
// goodprocessInput(input);String result = compute();use(result);// badString result;processInput(input);result = compute();use(result);Annotations
Each annotation on a class, method, or constructor goes on its own line.
@Override@Nullablepublic String getName() { // good ...}@Override @Nullable public String getName() { // bad ...}Annotations on fields may appear on the same line when there is only one.
@Nullable private String name; // okImports
Never use wildcard imports, it wastes resources.
import java.util.List;import java.util.Map; // goodimport java.util.*; // badImports are ordered as follows with a blank line separating each group:
- Static imports
java.*andjavax.*- Third-party libraries
- Internal/project imports
Order imports alphabetically within each group.
Commas
Always use trailing commas in multi-line enums and annotations.
enum Status { PENDING, ACTIVE, CLOSED, // <-- trailing comma}Strings
Always use double quotes for string literals. Single quotes are for char literals only.
String s = "hello"; char c = 'h';Comments
Use // for inline and single-line comments. Use /** */ Javadoc for all APIs.
For Javadoc, include description, parameters (@param), return value (@return), and errors thrown (@throws)
/** * Process a registration using a one-time token. * * @param token one-time registration token * @param request user email and first/last name captured from the form * @return response containing success, message and user email * @throws RegistrationException if token is invalid/expired or any step fails */public RegistrationResponse processRegistration(UUID token, RegistrationRequest request) { ...}Don't write comments that just restate the code:
i++; // increments i by 1 — badretryCount = 0; // reset retry counter after successful connection — good