Answer by user595447 for Why use getters and setters/accessors?
Thanks, that really clarified my thinking. Now here is (almost) 10 (almost) good reasons NOT to use getters and setters:When you realize you need to do more than just set and get the value, you can...
View ArticleAnswer by ebt for Why use getters and setters/accessors?
I wanted to post a real world example I just finished up:background - I hibernate tools to generate the mappings for my database, a database I am changing as I develop. I change the database schema,...
View ArticleAnswer by Thorbjørn Ravn Andersen for Why use getters and setters/accessors?
I spent quite a while thinking this over for the Java case, and I believe the real reasons are:Code to the interface, not the implementationInterfaces only specify methods, not fieldsIn other words,...
View ArticleAnswer by fastcodejava for Why use getters and setters/accessors?
I would just like to throw the idea of annotation : @getter and @setter. With @getter, you should be able to obj = class.field but not class.field = obj. With @setter, vice versa. With @getter and...
View ArticleAnswer by Kai for Why use getters and setters/accessors?
Lots of people talk about the advantages of getters and setters but I want to play devil's advocate. Right now I'm debugging a very large program where the programmers decided to make everything...
View ArticleAnswer by Antz for Why use getters and setters/accessors?
In an object oriented language the methods, and their access modifiers, declare the interface for that object. Between the constructor and the accessor and mutator methods it is possible for the...
View ArticleAnswer by quillbreaker for Why use getters and setters/accessors?
It can be useful for lazy-loading. Say the object in question is stored in a database, and you don't want to go get it unless you need it. If the object is retrieved by a getter, then the internal...
View ArticleAnswer by LBushkin for Why use getters and setters/accessors?
There are actually many good reasons to consider using accessors rather than directly exposing fields of a class - beyond just the argument of encapsulation and making future changes easier. Here are...
View ArticleAnswer by jdehaan for Why use getters and setters/accessors?
One aspect I missed in the answers so far, the access specification:for members you have only one access specification for both setting and gettingfor setters and getters you can fine tune it and...
View ArticleAnswer by jcdyer for Why use getters and setters/accessors?
Depends on your language. You've tagged this "object-oriented" rather than "Java", so I'd like to point out that ChssPly76's answer is language-dependent. In Python, for instance, there is no reason to...
View ArticleAnswer by Jason Baker for Why use getters and setters/accessors?
One other use (in languages that support properties) is that setters and getters can imply that an operation is non-trivial. Typically, you want to avoid doing anything that's computationally expensive...
View ArticleAnswer by Peter D for Why use getters and setters/accessors?
There are many reasons. My favorite one is when you need to change the behavior or regulate what you can set on a variable. For instance, lets say you had a setSpeed(int speed) method. But you want...
View ArticleAnswer by Pete for Why use getters and setters/accessors?
Additionally, this is to "future-proof" your class. In particular, changing from a field to a property is an ABI break, so if you do later decide that you need more logic than just "set/get the field",...
View ArticleAnswer by John Millikin for Why use getters and setters/accessors?
In languages which don't support "properties" (C++, Java) or require recompilation of clients when changing fields to properties (C#), using get/set methods is easier to modify. For example, adding...
View ArticleAnswer by Justin Niessner for Why use getters and setters/accessors?
One of the basic principals of OO design: Encapsulation!It gives you many benefits, one of which being that you can change the implementation of the getter/setter behind the scenes but any consumer of...
View ArticleAnswer by Thomas Owens for Why use getters and setters/accessors?
One advantage of accessors and mutators is that you can perform validation.For example, if foo was public, I could easily set it to null and then someone else could try to call a method on the object....
View ArticleAnswer by ChssPly76 for Why use getters and setters/accessors?
Because 2 weeks (months, years) from now when you realize that your setter needs to do more than just set the value, you'll also realize that the property has been used directly in 238 other classes :-)
View ArticleWhy use getters and setters/accessors?
What's the advantage of using getters and setters - that only get and set - instead of simply using public fields for those variables?If getters and setters are ever doing more than just the simple...
View Article