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.
About Question enthuware.ocpjp.ii.v11.2.3405 :
Moderators: Site Manager, fjwalraven
-
- Posts: 10
- Joined: Tue Jul 09, 2019 8:33 am
- Contact:
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.3405 :
Correct. If that were an option, it should be selected as well.
Added it now.
thank you for your feedback!
Added it now.
thank you for your feedback!
If you like our products and services, please help us by posting your review here.
-
- Posts: 8
- Joined: Sun Feb 23, 2020 6:51 am
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.3405 :
The guidelines also state (Guideline 6-1 / MUTABLE-1) that (emphasis from me):
For me, this means that in order to guarantee immutability, the class either: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.
- should be final
- should have only private constructors (this includes having at least one constructor, in order to not have a default constructor)
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.3405 :
Yes, your reasoning is correct. The explanation has now been enhanced to explain this point as well.
thank you for your feedback!
thank you for your feedback!
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 8 guests