I am validating input data in setter method and don't want to validate it again in constructor. I wonder if calling the setter in the constructor is good idea?
Asked
Active
Viewed 1,521 times
4 Answers
1
Yes, you can call setter in constructor.
Sample() {
setName("name");
}
it's not prohibited

Funtime
- 2,466
- 4
- 20
- 21
1
Generally considered safe, with the following caveat:
the only possible trouble you can get into is if the setter (or any method you call from a constructor) is overridden in a sub class. To be absolutely safe (paranoic?), make sure that all methods called from a constructor are final.

MeBigFatGuy
- 28,272
- 7
- 61
- 66