Hello there,
I've just gone through this section and got the doubt; is it convenient to call setters from the constructors?
The doctor recommends to use all the logic to determine whether the inputted data is valid/correct when an object gets created out of the constructor but how does one prevent an object from being given incorrect data via parameters?
if this is something covered on further lessons, please let me know so and I'll patiently wait for the topic to come up again. If not, I'd appreciate your time and efforts to answer my question.
Happy coding :)
hidden #10447638
Level 22
Do any of you call setters within constructors?
Resolved
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Dmitrii
31 May 2019, 12:25solution
That is up to you i believe, whether to use a setter or directly assign value, or even implement some logic in constructor for invalid data check, but i'll try to give an example in which you would want to use setters in constructor.
So lets say u have a field which can have a ton of invalid states, like a String representing an email for example, and u need to implement a complicated logic to check whether the value passed is valid or not, and also u need that value to be changable (have a setter), after creation of an object.
So u implement that logic in a setter, and u have a choice here to implement the same logic for the field in a constructor, or to just call a setter, i think u see which one is better easier etc.
+2
hidden #10447638
31 May 2019, 21:11
that was enlightening. It really helps ;) Thanks a lot for your time.
0