| name | java |
| description | Google's official Java style guide. Covers 2-space indentation, 100-char line limit, naming conventions, braces, imports, Javadoc, exception handling, lambdas, and streams. Enforces @Override annotations and specific imports. |
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
@Override whenever 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
public class Animal {
private final String name;
public Animal(String name) {
this.name = name;
}
public String speak() {
return name + " makes a sound.";
}
}
Control Structures
if (condition) {
doSomething();
}
if (condition)
doSomething();
for (String item : items) {
process(item);
}
Exception Handling
try {
processData(input);
} catch (IOException e) {
logger.error("IO error: {}", e.getMessage());
throw new ServiceException("Failed", e);
}
try (InputStream in = new FileInputStream(file)) {
return IOUtils.toByteArray(in);
}
try {
...
} catch (Exception e) {
e.printStackTrace();
}
Lambdas and Streams
List<String> names = users.stream()
.filter(u -> u.isActive())
.map(User::getName)
.sorted()
.collect(Collectors.toList());
users.forEach(System.out::println);
Javadoc
public Optional<User> findUserById(long userId) { ... }
Imports
import java.util.List;
import java.util.Optional;
import java.util.*;
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.