Macan's Blog

chaotic thoughts


Effective Java

Creating and Destroying Objects

  1. Consider static factory methods instead of constructors
  2. Consider a builder when faced with many constructor parameters
  3. Enforce the singleton property with a private constructor or an enum type
  1. Enforce non-instantiated with a private constructor
  2. Prefer dependency injection to hard-writing resources
  3. Avoid creating unnecessary objects.
  4. Eliminate obsolete object references
  5. Avoid finalizers and cleaners
  6. Prefer try-with-resources to try-finally

Methods Common to All Objects

  1. Obey the general contract when overriding equals
  2. Always override hashCode when you override equals
  3. Always override toString
  4. Override clone judiciously
  5. Considering implementing Comparable

Classes and Interfaces

  1. Minimize the accessibility of classes and members
  2. In public classes, use accessor methods, not public fields
  3. Minimize mutability
  4. Favor composition over inheritance
  5. 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.
  1. Prefer interfaces to abstract classes
  2. Design interfaces for posterity
  3. Use interfaces only to define types
  4. Prefer class hierarchies to tagged classes
  5. Favor static member classes over non-static
  6. Limit source files to a single top-level class


  1. Don’t use raw types
  2. Eliminate unchecked warnings
  3. Prefer lists to arrays
  4. Favor generic types
  5. Favor generic methods
  6. Use bounded wildcard to increase API flexibility
  7. Combine generics and var-args judiciously
  8. Consider type safe heterogeneous containers

Enums and Annotations

  1. Use enums instead of int constants
  2. USe instance fields instead of ordinals
  3. Use EnumSet instead of bit fields
  4. Use EnumMap instead of ordinal indexing
  5. Emulate extensible enums with interfaces
  6. Prefer annotations to naming patterns
  7. Consistently use the Override annotations
  8. Use marker interfaces to define types

Lambdas and Streams

  1. Prefer lambdas to anonymous classes
  2. Prefer method references to lambdas
  3. Favor the use of standard functional interfaces
  4. Use stream judiciously
  5. Prefer side-effect-free functions in streams