Can I write validation logic in setter methods?

后端 未结 7 1073
自闭症患者
自闭症患者 2020-12-09 17:49

Are setter methods only used to set the value of attributes as it is passed as argument? Can we write some validation logic before assigning the value to the attributes?

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-09 18:20

    As long as you do not modify other fields of class it is correct to validate.

    You should also consider removing setters and using constructor with valitation or builder in Joshua Bloh version

提交回复
热议问题