About Question enthuware.ocpjp.ii.v11.2.3405 :

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
BinSlayer
Posts: 10
Joined: Tue Jul 09, 2019 8:33 am
Contact:

About Question enthuware.ocpjp.ii.v11.2.3405 :

Post by BinSlayer »

I see 2 reasons why this class is not immutable.

1) The getMaturity method should return a clone of maturityDate. This is indeed the answer highlighted by Enthuware software.

2) I can pass a java.util.Date to the build() method and then I can modify this Date in my own code context. Because the constructor directly assigns a mutable parameter to a mutable field (this.maturityDate = matDate;) . A clone should be made here.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.3405 :

Post by admin »

Correct. If that were an option, it should be selected as well.
Added it now.
thank you for your feedback!
If you like our products and services, please help us by posting your review here.

FlatPanda
Posts: 8
Joined: Sun Feb 23, 2020 6:51 am
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.3405 :

Post by FlatPanda »

The guidelines also state (Guideline 6-1 / MUTABLE-1) that (emphasis from me):
Immutable classes should not be subclassable. Further, hiding constructors allows more flexibility in instance creation and caching. This means making the constructor private or default access ("package-private"), or being in a package controlled by the package.access security property.
For me, this means that in order to guarantee immutability, the class either:
  • should be final
  • should have only private constructors (this includes having at least one constructor, in order to not have a default constructor)
In this question the class being final is not a required answer, because by having a private constructor with arguments there is no default constructor generated and thus the class is not extensible regardless of being final or not. Is my reasoning correct here?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.3405 :

Post by admin »

Yes, your reasoning is correct. The explanation has now been enhanced to explain this point as well.

thank you for your feedback!
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 20 guests