Foreword |
|
xi | |
Preface |
|
xiii | |
Acknowledgments |
|
xv | |
|
|
1 | (4) |
|
Creating and Destroying Objects |
|
|
5 | (20) |
|
Consider providing static factory methods instead of constructors |
|
|
5 | (5) |
|
Enforce the singleton property with a private constructor |
|
|
10 | (2) |
|
Enforce noninstantiability with a private constructor |
|
|
12 | (1) |
|
Avoid creating duplicate objects |
|
|
13 | (4) |
|
Eliminate obsolete object references |
|
|
17 | (3) |
|
|
20 | (5) |
|
Methods Common to All Objects |
|
|
25 | (34) |
|
Obey the general contract when overriding equals |
|
|
25 | (11) |
|
Always override hashCode when you override equals |
|
|
36 | (6) |
|
Always override to String |
|
|
42 | (3) |
|
Override clone judiciously |
|
|
45 | (8) |
|
Consider implementing Comparable |
|
|
53 | (6) |
|
|
59 | (38) |
|
Minimize the accessibility of classes and members |
|
|
59 | (4) |
|
|
63 | (8) |
|
Favor composition over inheritance |
|
|
71 | (7) |
|
Design and document for inheritance or else prohibit it |
|
|
78 | (6) |
|
Prefer interfaces to abstract classes |
|
|
84 | (5) |
|
Use interfaces only to define types |
|
|
89 | (2) |
|
Favor static member classes over nonstatic |
|
|
91 | (6) |
|
Substitutes for C Constructs |
|
|
97 | (22) |
|
Replace structures with classes |
|
|
97 | (3) |
|
Replace unions with class hierarchies |
|
|
100 | (4) |
|
Replace enum constructs with classes |
|
|
104 | (11) |
|
Replace function pointers with classes and interfaces |
|
|
115 | (4) |
|
|
119 | (22) |
|
Check parameters for validity |
|
|
119 | (3) |
|
Make defensive copies when needed |
|
|
122 | (4) |
|
Design method signatures carefully |
|
|
126 | (2) |
|
Use overloading judiciously |
|
|
128 | (6) |
|
Return zero-length arrays, not nulls |
|
|
134 | (2) |
|
Write doc comments for all exposed API elements |
|
|
136 | (5) |
|
|
141 | (28) |
|
Minimize the scope of local variables |
|
|
141 | (4) |
|
Know and use the libraries |
|
|
145 | (4) |
|
Avoid float and double if exact answers are required |
|
|
149 | (3) |
|
Avoid strings where other types are more appropriate |
|
|
152 | (3) |
|
Beware the performance of string concatenation |
|
|
155 | (1) |
|
Refer to objects by their interfaces |
|
|
156 | (2) |
|
Prefer interfaces to reflection |
|
|
158 | (3) |
|
Use native methods judiciously |
|
|
161 | (1) |
|
|
162 | (3) |
|
Adhere to generally accepted naming conventions |
|
|
165 | (4) |
|
|
169 | (20) |
|
Use exceptions only for exceptional conditions |
|
|
169 | (3) |
|
Use checked exceptions for recoverable conditions and run-time exceptions for programming errors |
|
|
172 | (2) |
|
Avoid unnecessary use of checked exceptions |
|
|
174 | (2) |
|
Favor the use of standard exceptions |
|
|
176 | (2) |
|
Throw exceptions appropriate to the abstraction |
|
|
178 | (3) |
|
Document all exceptions thrown by each method |
|
|
181 | (2) |
|
Include failure-capture information in detail messages |
|
|
183 | (2) |
|
Strive for failure atomicity |
|
|
185 | (2) |
|
|
187 | (2) |
|
|
189 | (24) |
|
Synchronize access to shared mutable data |
|
|
189 | (7) |
|
Avoid excessive synchronization |
|
|
196 | (5) |
|
Never invoke wait outside a loop |
|
|
201 | (3) |
|
Don't depend on the thread scheduler |
|
|
204 | (4) |
|
|
208 | (3) |
|
|
211 | (2) |
|
|
213 | (20) |
|
Implement Serializable judiciously |
|
|
213 | (5) |
|
Consider using a custom serialized form |
|
|
218 | (6) |
|
Write readObject methods defensively |
|
|
224 | (6) |
|
Provide a readResolve method when necessary |
|
|
230 | (3) |
References |
|
233 | (6) |
Index of Patterns and Idioms |
|
239 | (2) |
Index |
|
241 | |