In addition to protecting getDateOfBirth
as @rolfl @rolfl already explained,
it's equally important to make defensive copies in constructors too:
Person(String name, Date dateOfBirth) {
this.name = name;
this.dateOfBirth = new Date(dateOfBirth.getTime());
}
Otherwise, if you do simply this.dateOfBirth = dateOfBirth;
, then somebody could write this code:
Person jack = new Person("Jack", date);
date.setYear(1900); // ouch!
Since mutable objects such as Date
are so troublesome,
it's a lot safer to store the long
value of dateOfBirth.getTime()
instead of the Date
itself.
That way you simply don't need to remember to use defensive copies,
your long
value will always remain immutable, as opposed to Date
objects.
If you want the safest possible immutable class, then you should definitely go with this option, and store the long
values representing dates. The same goes for dateOfJoining
in Employee
as well.
In addition to protecting getDateOfBirth
as @rolfl already explained,
it's equally important to make defensive copies in constructors too:
Person(String name, Date dateOfBirth) {
this.name = name;
this.dateOfBirth = new Date(dateOfBirth.getTime());
}
Otherwise, if you do simply this.dateOfBirth = dateOfBirth;
, then somebody could write this code:
Person jack = new Person("Jack", date);
date.setYear(1900); // ouch!
Since mutable objects such as Date
are so troublesome,
it's a lot safer to store the long
value of dateOfBirth.getTime()
instead of the Date
itself.
That way you simply don't need to remember to use defensive copies,
your long
value will always remain immutable, as opposed to Date
objects.
If you want the safest possible immutable class, then you should definitely go with this option, and store the long
values representing dates. The same goes for dateOfJoining
in Employee
as well.
In addition to protecting getDateOfBirth
as @rolfl already explained,
it's equally important to make defensive copies in constructors too:
Person(String name, Date dateOfBirth) {
this.name = name;
this.dateOfBirth = new Date(dateOfBirth.getTime());
}
Otherwise, if you do simply this.dateOfBirth = dateOfBirth;
, then somebody could write this code:
Person jack = new Person("Jack", date);
date.setYear(1900); // ouch!
Since mutable objects such as Date
are so troublesome,
it's a lot safer to store the long
value of dateOfBirth.getTime()
instead of the Date
itself.
That way you simply don't need to remember to use defensive copies,
your long
value will always remain immutable, as opposed to Date
objects.
If you want the safest possible immutable class, then you should definitely go with this option, and store the long
values representing dates. The same goes for dateOfJoining
in Employee
as well.
In addition to protecting getDateOfBirth
as @rolfl already explained,
it's equally important to make defensive copycopies in the constructorconstructors too:
Person(String name, Date dateOfBirth) {
this.name = name;
this.dateOfBirth = new Date(dateOfBirth.getTime());
}
Otherwise, if you do simply this.dateOfBirth = dateOfBirth;
, then somebody could write this code:
Person jack = new Person("Jack", date);
date.setYear(1900); // ouch!
Since mutable objects such as Date
are so troublesome,
it's a lot safer to store the long
value of dateOfBirth.getTime()
instead of the Date
itself.
That way you simply don't need to remember to use defensive copies,
your long
value will always remain immutable, as opposed to Date
objects.
If you want the safest possible immutable class, then you should definitely go with this option, and store the long
values representing dates. The same goes for dateOfJoining
in Employee
as well.
In addition to protecting getDateOfBirth
as @rolfl already explained,
it's equally important to make defensive copy in the constructor too:
Person(String name, Date dateOfBirth) {
this.name = name;
this.dateOfBirth = new Date(dateOfBirth.getTime());
}
Otherwise, if you do simply this.dateOfBirth = dateOfBirth;
, then somebody could write this code:
Person jack = new Person("Jack", date);
date.setYear(1900);
Since mutable objects such as Date
are so troublesome,
it's a lot safer to store the long
value of dateOfBirth.getTime()
instead of the Date
itself.
That way you simply don't need to remember to use defensive copies,
your long
value will always remain immutable, as opposed to Date
objects.
If you want the safest possible immutable class, then you should definitely go with this option, and store the long
values representing dates. The same goes for dateOfJoining
in Employee
as well.
In addition to protecting getDateOfBirth
as @rolfl already explained,
it's equally important to make defensive copies in constructors too:
Person(String name, Date dateOfBirth) {
this.name = name;
this.dateOfBirth = new Date(dateOfBirth.getTime());
}
Otherwise, if you do simply this.dateOfBirth = dateOfBirth;
, then somebody could write this code:
Person jack = new Person("Jack", date);
date.setYear(1900); // ouch!
Since mutable objects such as Date
are so troublesome,
it's a lot safer to store the long
value of dateOfBirth.getTime()
instead of the Date
itself.
That way you simply don't need to remember to use defensive copies,
your long
value will always remain immutable, as opposed to Date
objects.
If you want the safest possible immutable class, then you should definitely go with this option, and store the long
values representing dates. The same goes for dateOfJoining
in Employee
as well.
In addition to protecting getDateOfBirth
as @rolfl already explained,
it's equally important to make defensive copy in the constructor too:
Person(String name, Date dateOfBirth) {
this.name = name;
this.dateOfBirth = new Date(dateOfBirth.getTime());
}
Without thatOtherwise, if you do simply this.dateOfBirth = dateOfBirth;
, then somebody could write this code:
Person jack = new Person("Jack", date);
date.setYear(1900);
Since mutable objects such as Date
are so troublesome,
it's a lot safer to store the long
value of dateOfBirth.getTime()
instead of the Date
itself.
That way you simply don't need to remember to use defensive copies,
your long
value will always remain immutable, as opposed to Date
objects.
If you want the safest possible immutable class, then you should definitely go with this option, and store the long
values representing dates. The same goes for dateOfJoining
in Employee
as well.
In addition to protecting getDateOfBirth
as @rolfl already explained,
it's equally important to make defensive copy in the constructor too:
Person(String name, Date dateOfBirth) {
this.name = name;
this.dateOfBirth = new Date(dateOfBirth.getTime());
}
Without that, somebody could write this code:
Person jack = new Person("Jack", date);
date.setYear(1900);
Since mutable objects such as Date
are so troublesome,
it's a lot safer to store the long
value of dateOfBirth.getTime()
instead of the Date
itself.
That way you simply don't need to remember to use defensive copies,
your long
value will always remain immutable, as opposed to Date
objects.
If you want the safest possible immutable class, then you should definitely go with this option, and store the long
values representing dates. The same goes for dateOfJoining
in Employee
as well.
In addition to protecting getDateOfBirth
as @rolfl already explained,
it's equally important to make defensive copy in the constructor too:
Person(String name, Date dateOfBirth) {
this.name = name;
this.dateOfBirth = new Date(dateOfBirth.getTime());
}
Otherwise, if you do simply this.dateOfBirth = dateOfBirth;
, then somebody could write this code:
Person jack = new Person("Jack", date);
date.setYear(1900);
Since mutable objects such as Date
are so troublesome,
it's a lot safer to store the long
value of dateOfBirth.getTime()
instead of the Date
itself.
That way you simply don't need to remember to use defensive copies,
your long
value will always remain immutable, as opposed to Date
objects.
If you want the safest possible immutable class, then you should definitely go with this option, and store the long
values representing dates. The same goes for dateOfJoining
in Employee
as well.