A. The MyCircle class is fully encapsulated.
Totally wrong. The class members are public and therefore, not encapsulated.
B. The diameter of a given MyCircle is guaranteed to be twice its radius.
Again wrong. The members are public and therefore, open to abrupt change. Moreover, no constructor means that the radius == diameter for a newly constructed object. Thus there is no guarantee at all.
C. Lines 6 and 7 should be in a synchronized block to ensure encapsulation.
The method
setRadius() should be synchronized if it will be accessed by multiple threads simultaneously, to prevent a
"data race". This is illustrated in the following two figures, taken from the JLS.
For explanation & details, see
Section 17.4.5 in the JLS.
But still, this option isn't correct, because, this doesn't seem to be related with encapsulation.
Looking at this from another perspective, if the method
setRadius() isn't synchronized & is accessed by multiple threads at the same time, it will result in disaster. The radius & diameter variables may get inconsistent/incorrect values due to this. Will this break/affect the encapsulation then? Anyone on this...
D. The radius of a MyCircle object can be set without affecting its diameter.
Yes, indeed. Both instance variables are public and therefore, can be changed in any manner whatsoever.
A & B are incorrect & D is correct, undoubtedly. I am unsure about option C.
Best regards,
Abdul Rehman.
[ November 24, 2006: Message edited by: Abdul Rehman ]