Encapsulation. Is the following class immutable? Answer: No. Java’s java. util. Date class is mutable. The method set Date(seconds) changes the value of the invoking date to the number of milliseconds...


Encapsulation. Is the following class immutable?


Answer: No. Java’s java. util. Date class is mutable. The method set Date(seconds) changes the value of the invoking date to the number of milliseconds since January 1, 1970, 00:00:00 GMT. This has the unfortunate consequence that when a client gets a date with date = get Date(), the client program can then invoke date. set Date() and change the date in an Appointment object type, perhaps creating a conflict. In a data type, we cannot let references to mutable objects escape because the caller can then modify its state. One solution is to create a defensive copy of the Date before returning it using new Date(date. get Time()); and a defensive copy when storing it via this. date = new Date(date. get Time()). Many programmers regard the mutability of Date as a Java design flaw. (Gregorian Calendar is a more modern Java library for storing dates, but it is mutable, too.)



Nov 28, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here