This document serves as the complete definition of Google's coding standards for source code in the Java™ Programming Language.
- class includes normal class, record class, enum class, interface or annotation type (
@interface). - member includes nested class, field, method, or constructor.
- comment refers to implementation comments (not Javadoc).
The file name equals the case-sensitive name of the top-level class plus .java.
Source files are encoded in UTF-8.
- Only ASCII horizontal space (0x20) is used for whitespace (no tabs).
- Special escape sequences (
\b,\t,\n,\f,\r,\s,\",\',\\) are used instead of octal/Unicode escapes.
In order:
- License or copyright information
- Package declaration (not line-wrapped)
- Imports
- Exactly one top-level class
Exactly one blank line separates each section.
- No wildcard imports — static or otherwise.
- No module imports.
- No line-wrapping of imports.
- Order: static imports first, then non-static imports, separated by one blank line. Within each group, ASCII sort order.
- Static import is not used for static nested classes.
- Each top-level class resides in its own source file.
- Members/initializers follow some logical order.
- Overloads: never split — methods/constructors with the same name appear in a single contiguous group.
Braces are used with if, else, for, do and while even when the body is empty or single-statement.
- No line break before the opening brace.
- Line break after the opening brace.
- Line break before the closing brace.
- Line break after the closing brace only if it terminates a statement or the body of a method, constructor, or named class.
May be concise ({}) unless part of a multi-block statement (if/else, try/catch/finally).
Exceptions: package declarations, imports, text blocks, command lines in comments, very long identifiers.
- Break at a higher syntactic level.
- Break before non-assignment operators (
.,::,|,&in type bounds). - Break after assignment operators (either side is acceptable).
- Method/constructor name stays attached to the opening
(. - A comma stays attached to the preceding token.
- Never break adjacent to lambda
->or switch rule->, except immediately after it for a single unbraced expression.
A single blank line appears:
- Between consecutive members or initializers of a class.
- Between sections as required by other rules. Multiple consecutive blank lines are permitted but never required.
A single ASCII space appears:
- After keywords (
if,for,catch) before( - Before
{(with exceptions) - On both sides of binary/ternary operators (except
.and::) - After
,,;,),// - Between type and identifier in declarations
Optional line break after commas. May be formatted like an array initializer if no methods/docs.
- One variable per declaration (exception:
forloop header). - Declared close to first use, typically with an initializer.
- C-style array declarations are not used (
String[] args, notString args[]). - Array initializers may be formatted like block-like constructs.
- Contents indented +2, labels at +2.
- Fall-through must be commented (
// fall through). - Every switch must be exhaustive (include
defaulteven if empty). - Switch expressions must use new-style (
->).
- Class/package/module annotations: one per line.
- Method/constructor annotations: one per line (exception: single parameterless annotation may share the line).
- Field annotations: multiple may appear on the same line.
- Block comments indented at the same level as surrounding code.
TODOcomments: format// TODO: <resource> - <description>.
Order: public protected private abstract default static final sealed non-sealed transient volatile synchronized native strictfp
long suffix is uppercase L.
ASCII letters and digits only (underscores in rare cases). No special prefixes or suffixes (name_, mName, s_name, kName).
| Type | Style | Examples |
|---|---|---|
| Package/module | Lowercase only, concatenated | com.example.deepspace |
| Class | UpperCamelCase | Character, ImmutableList |
| Method | lowerCamelCase | sendMessage, stop |
| Constant | UPPER_SNAKE_CASE | MAX_VALUE, NAMES |
| Non-constant field | lowerCamelCase | computedValues, index |
| Parameter | lowerCamelCase | input, label |
| Local variable | lowerCamelCase | result, i |
| Type variable | Single capital letter or class-style + T |
E, T, RequestT |
| Unnamed variable | _ |
Predicate<String> alwaysTrue = _ -> true; |
- Convert to plain ASCII, remove apostrophes.
- Split into words on spaces/punctuation.
- Lowercase everything, then uppercase first character of each word (UpperCamelCase) or each word except the first (lowerCamelCase).
- Join words.
Whenever a method overrides a supertype method, implements an interface method, or respecifies a superinterface method.
If truly appropriate to take no action, explain in a comment.
Foo.aStaticMethod(), not aFoo.aStaticMethod().
Do not override Object.finalize.
- Basic form:
/** ... */with aligned*on continuation lines. - Single-line form acceptable when everything (including tags) fits on one line.
Brief noun phrase or verb phrase (not a complete sentence), capitalized and punctuated as if it were a complete sentence.
Required for every visible class, member, or record component, except:
- Self-explanatory members (e.g., simple
getFoo()). - Methods that override a supertype method.