- Is there, besides the principles of encapsulation, the criteria for determining the need for heterosexuals and setters, or can it be applied to a straight line and field? And why do I sometimes see heteras and setters for public fields? In addition to checks. This is understandable ... There are more reasons why this may be needed?
- oneAnd what does the mysterious number 7 in your question mean? - VladD
|
1 answer
Having getters and setters, you provide an open interface to users of your class, which will not depend on how getters and setters are implemented. This gives flexibility in the design. You can change the implementation of getters and setters, but the user code will not change.
Of course, you should choose one thing: either getters and setters, or open fields. Otherwise, the meaning is lost.
- >> Of course, you should choose one thing: either getters and setters, or open fields. Otherwise, the meaning is lost. << - Pavel
- So I always thought so, but here I met getters and setters for open fields and was surprised, I thought I misunderstand something fundamentally ... - Pavel
- one@Pavel most likely you have entered the code of a developer of insufficient qualification - pavlofff
- @Pavel, there are many options why getters and setters meet with the public field, but basically it all boils down to either insufficient qualifications, as mentioned above, or lack of time to do it right. For example, I had this when I used the gson library, for serialization in json, the getters / setters were declared in the interfaces, but in the gson object itself I allowed to specify anotations only to the fields (although I still refused to just gson in favor of jackson) - Evgenii Izhboldin
|