-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathPerson1.java
More file actions
36 lines (28 loc) · 946 Bytes
/
Person1.java
File metadata and controls
36 lines (28 loc) · 946 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Creates lots of unnecessary duplicate objects - page 20-21
package effectivejava.chapter2;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
public class Person1 {
private final Date birthDate;
public Person1(Date birthDate) {
// Defensive copy - see Item 39
this.birthDate = new Date(birthDate.getTime());
}
// Other fields, methods omitted
// DON'T DO THIS!
public boolean isBabyBoomer() {
// Unnecessary allocation of expensive object
Calendar gmtCal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
gmtCal.set(1946, Calendar.JANUARY, 1, 0, 0, 0);
Date boomStart = gmtCal.getTime();
gmtCal.set(1965, Calendar.JANUARY, 1, 0, 0, 0);
Date boomEnd = gmtCal.getTime();
return birthDate.compareTo(boomStart) >= 0
&& birthDate.compareTo(boomEnd) < 0;
}
public static void main(String[] args) {
Person1 p = new Person1(new Date());
System.out.println(p.isBabyBoomer());
}
}