0%
Creating and Destroying Objects
- Consider static factory methods instead of constructors
- Consider a builder when faced with many constructor parameters
- Enforce the singleton property with a private constructor or an enum type
- Enforce non-instantiated with a private constructor
- Prefer dependency injection to hard-writing resources
- Avoid creating unnecessary objects.
- Eliminate obsolete object references
- Avoid finalizers and cleaners
- Prefer try-with-resources to try-finally
Methods Common to All Objects
- Obey the general contract when overriding equals
- Always override hashCode when you override equals
- Always override toString
- Override clone judiciously
- Considering implementing Comparable
Classes and Interfaces
- Minimize the accessibility of classes and members
- In public classes, use accessor methods, not public fields
- Minimize mutability
- Favor composition over inheritance
- Design and document for inheritance or else prohibit it
- Good API documentation should describe what a given method does and not how it does it.
- Prefer interfaces to abstract classes
- Design interfaces for posterity
- Use interfaces only to define types
- Prefer class hierarchies to tagged classes
- Favor static member classes over non-static
- Limit source files to a single top-level class
Generics
- Don’t use raw types
- Eliminate unchecked warnings
- Prefer lists to arrays
- Favor generic types
- Favor generic methods
- Use bounded wildcard to increase API flexibility
- Combine generics and var-args judiciously
- Consider type safe heterogeneous containers
Enums and Annotations
- Use enums instead of int constants
- USe instance fields instead of ordinals
- Use EnumSet instead of bit fields
- Use EnumMap instead of ordinal indexing
- Emulate extensible enums with interfaces
- Prefer annotations to naming patterns
- Consistently use the Override annotations
- Use marker interfaces to define types
Lambdas and Streams
- Prefer lambdas to anonymous classes
- Prefer method references to lambdas
- Favor the use of standard functional interfaces
- Use stream judiciously
- Prefer side-effect-free functions in streams