java
Google Java Style Guide
Official Google Java coding standards for consistent, maintainable code.
Golden Rules
- 2-space indentation — no tabs
- Column limit: 100 characters
- Use
@Overridewhenever applicable - No wildcard imports — import specific types
- Braces required even for single-statement blocks
- One top-level class per file
- Prefer interfaces for type definitions
Quick Reference
Naming Conventions
| Element | Convention | Example |
|---|---|---|
| Packages | lowercase.dotted | com.example.project |
| Classes/Interfaces | UpperCamelCase | UserService |
| Methods | lowerCamelCase | getUserById |
| Variables | lowerCamelCase | userCount |
| Constants | UPPER_SNAKE_CASE | MAX_RETRIES |
| Type parameters | Single letter | T, RequestT |
Classes
// ✓ CORRECT
public class Animal {
private final String name;
public Animal(String name) {
this.name = name;
}
public String speak() {
return name + " makes a sound.";
}
}
Control Structures
// ✓ CORRECT - braces always required
if (condition) {
doSomething();
}
// ✗ INCORRECT - no braces
if (condition)
doSomething();
// ✓ CORRECT - enhanced for loop
for (String item : items) {
process(item);
}
Exception Handling
// ✓ CORRECT
try {
processData(input);
} catch (IOException e) {
logger.error("IO error: {}", e.getMessage());
throw new ServiceException("Failed", e);
}
// ✓ CORRECT - try-with-resources
try (InputStream in = new FileInputStream(file)) {
return IOUtils.toByteArray(in);
}
// ✗ INCORRECT
try {
...
} catch (Exception e) { // too broad
e.printStackTrace(); // don't use printStackTrace
}
Lambdas and Streams
// ✓ CORRECT
List<String> names = users.stream()
.filter(u -> u.isActive())
.map(User::getName)
.sorted()
.collect(Collectors.toList());
// ✓ CORRECT - method references
users.forEach(System.out::println);
Javadoc
/**
* Finds a user by their unique identifier.
*
* @param userId the user's unique ID
* @return an Optional containing the user, or empty if not found
*/
public Optional<User> findUserById(long userId) { ... }
Imports
// ✓ CORRECT
import java.util.List;
import java.util.Optional;
// ✗ INCORRECT
import java.util.*; // no wildcard imports
Common Mistakes
| Mistake | Correct Approach |
|---|---|
| Wildcard imports | Specific imports only |
| Omitting braces | Always use braces |
| Missing @Override | Always annotate overridden methods |
| e.printStackTrace() | Use a proper logger |
| Catching Exception broadly | Catch specific exceptions |
| 4-space indentation | Use 2-space indentation |
When to Use This Guide
- Writing new Java code
- Refactoring existing Java
- Code reviews
- Setting up Checkstyle rules
- Onboarding new team members
Install
npx skills add testdino-hq/google-styleguides-skills/java
Full Guide
See java.md for complete details, examples, and edge cases.
More from testdino-hq/google-styleguides-skills
python
Google's official Python style guide extending PEP 8. Covers type annotations, Google-style docstrings, imports, naming conventions, f-strings, comprehensions, and exception handling. Enforces 80-char line length and 4-space indentation.
3shell
Google's official Shell scripting style guide. Covers Bash scripting, naming conventions, error handling, portability, and shell best practices.
1cpp
Google's official C++ style guide. Covers headers, naming conventions, formatting, classes, memory management, RAII, smart pointers, and modern C++ features.
1google-styleguides-skills
Complete collection of Google's official style guides for 17 languages. Includes TypeScript, JavaScript, Python, Java, Go, C++, C#, Swift, Objective-C, HTML/CSS, AngularJS, Shell, R, Common Lisp, Vim Script, JSON, and Markdown. Production-ready coding standards used across Google's engineering organization, formatted for AI agent consumption.
1