Java Language Oracle Official Code Standard Whitespace


Vertical Whitespace

  • A single blank line should be used to separate…

    • Package declaration
    • Class declarations
    • Constructors
    • Methods
    • Static initializers
    • Instance initializers
  • …and may be used to separate logical groups of

    • import statements
    • fields
    • statements
  • Multiple consecutive blank lines should only be used to separate groups of related members and not as the standard inter-member spacing.

Horizontal Whitespace

  • A single space should be used…

    • To separate keywords from neighboring opening or closing brackets and braces
    • Before and after all binary operators and operator like symbols such as arrows in lambda expressions and the colon in enhanced for loops (but not before the colon of a label)
    • After // that starts a comment.
    • After commas separating arguments and semicolons separating the parts of a for loop.
    • After the closing parenthesis of a cast.
  • In variable declarations it is not recommended to align types and variables.