Google has recently released their complete definition of coding standards for Java source code. These are hard-and-fast rules that are clearly enforceable, and are followed universally within Google. It covers not only formatting, but other types of conventions and coding standards.
The document is broken up into 6 main sections: Source File Basics, Source File Structure, Formatting, Naming, Programming Practices, and Javadoc. Source File Basics talks about filenames, file encoding, whitespace characters, and special characters. Source File Structure talks about license information, package and import statements, and class member ordering. Formatting talks about braces, indents, line wrapping, whitespace, parentheses, enums, arrays, switch statements, annotations, comments, and modifiers. Naming talks about identifiers (package, class, method, constant, field, local variable, type variable) and defines CamelCase. Programming Practices talks about @Override, exceptions, static members and finalizers. Javadoc talks about how to format Javadoc and where it is required.
Here are a few items contained in the guide.
- No wildcard imports.
- Overloads appear sequentially.
- Braces are used even when the body is empty or contains a single statement.
- 2 spaces indentation.
- Column limit can be 80 or 100 characters.
- No C-style array declarations.
- The default statement in switch statements are required.
- Modifiers appear in the order recommended by the Java Language Specification.
- Constants use CONSTANT_CASE. Note that every constant is a static final field, but not all static final fields are constants.
For further reading, please read the Google Java Style. There is also the official Code Conventions for the Java Programming Language from Oracle. Google also has style guides for other languages like C++, Objective-C, Python, Shell, HTML/CSS, JavaScript, and Lisp.