S
Saurabh
Hi,
Can somebody please help me with this? I am trying to figure out the
difference between this piece of code:
class Person {
private final Date birthDate;
public Person(Date birthDate) {
this.birthDate = birthDate;
}
private static final Date BOOM_START;
static {
Calendar gmtCal =
Calendar.getInstance(TimeZone.getTimeZone("GMT"));
gmtCal.set(1946, Calendar.JANUARY, 1, 0, 0, 0);
BOOM_START = gmtCal.getTime();
}
public boolean isBabyBoomer() {
..
}
}
and this:
class Person {
private final Date birthDate;
public Person(Date birthDate) {
this.birthDate = birthDate;
Calendar gmtCal =
Calendar.getInstance(TimeZone.getTimeZone("GMT"));
gmtCal.set(1946, Calendar.JANUARY, 1, 0, 0, 0);
BOOM_START = gmtCal.getTime();
}
private static final Date BOOM_START;
public boolean isBabyBoomer() {
..
}
}
The code example is a truncated version of some code from Effective
Java. I have been trying to figure out if there would be any
difference between the two. And why you would want to put some code in
static{} instead of contructors.
Thanks
Saurabh
Can somebody please help me with this? I am trying to figure out the
difference between this piece of code:
class Person {
private final Date birthDate;
public Person(Date birthDate) {
this.birthDate = birthDate;
}
private static final Date BOOM_START;
static {
Calendar gmtCal =
Calendar.getInstance(TimeZone.getTimeZone("GMT"));
gmtCal.set(1946, Calendar.JANUARY, 1, 0, 0, 0);
BOOM_START = gmtCal.getTime();
}
public boolean isBabyBoomer() {
..
}
}
and this:
class Person {
private final Date birthDate;
public Person(Date birthDate) {
this.birthDate = birthDate;
Calendar gmtCal =
Calendar.getInstance(TimeZone.getTimeZone("GMT"));
gmtCal.set(1946, Calendar.JANUARY, 1, 0, 0, 0);
BOOM_START = gmtCal.getTime();
}
private static final Date BOOM_START;
public boolean isBabyBoomer() {
..
}
}
The code example is a truncated version of some code from Effective
Java. I have been trying to figure out if there would be any
difference between the two. And why you would want to put some code in
static{} instead of contructors.
Thanks
Saurabh