DEV Community

Discussion on: Avoid getters and setters whenever possible

Collapse
 
hohonuuli profile image
Brian Schlining

tl;dr: Getters/Setters aren't evil. They are a tool.

Good lord, that's an awful title. People are going to only read that and think" I shouldn't use getters/setters". Please don't follow that advice. I would argue that in Java, NEVER, NEVER, NEVER access a public field of an instance. Always go through an accessor method. Accessors/mutators also can be defined in interfaces, allowing your software to enforce code contracts.

So yes, if you don't need to expose a field, don't. If you can make field immutable, that's great!! But still use an accessor to get it.