Quantcast
Channel: Why use getters and setters/accessors? - Stack Overflow
Browsing latest articles
Browse All 38 View Live

Answer by user982042 for Why use getters and setters/accessors?

If you don't require any validations and not even need to maintain state i.e. one property depends on another so we need to maintain the state when one is change. You can keep it simple by making field...

View Article



Answer by aditya lath for Why use getters and setters/accessors?

There is a difference between DataStructure and Object.Datastructure should expose its innards and not behavior.An Object should not expose its innards but it should expose its behavior, which is also...

View Article

Answer by Lakindu Hewawasam for Why use getters and setters/accessors?

From my experience, it is ideal to set variables as private and to provide accessors and modifiers to each variable.This way, you can create read only variables, or write only variables depending on...

View Article

Answer by Blasanka for Why use getters and setters/accessors?

Getters and setters coming from data hiding. Data Hiding means We are hiding data from outsiders or outside person/thing cannot access our data.This is a useful feature in OOP.As a example:If you...

View Article

Answer by Rakesh Singh for Why use getters and setters/accessors?

One relatively modern advantage of getters/setters is that is makes it easier to browse code in tagged (indexed) code editors. E.g. If you want to see who sets a member, you can open the call hierarchy...

View Article


Answer by kangalio for Why use getters and setters/accessors?

I know it's a bit late, but I think there are some people who are interested in performance.I've done a little performance test. I wrote a class "NumberHolder" which, well, holds an Integer. You can...

View Article

Answer by Jean-Christophe Blanchard for Why use getters and setters/accessors?

Although not common for getter and setter, the use of these methods can also be used in AOP/proxy pattern uses.eg for auditing variable you can use AOP to audit update of any value.Without...

View Article

Answer by Pritam Banerjee for Why use getters and setters/accessors?

Getters and setters are used to implement two of the fundamental aspects of Object Oriented Programming which are: AbstractionEncapsulationSuppose we have an Employee class:package...

View Article


Answer by Haakon Løtveit for Why use getters and setters/accessors?

EDIT: I answered this question because there are a bunch of people learning programming asking this, and most of the answers are very technically competent, but they're not as easy to understand if...

View Article


Answer by yegor256 for Why use getters and setters/accessors?

In a pure object-oriented world getters and setters is a terrible anti-pattern. Read this article: Getters/Setters. Evil. Period. In a nutshell, they encourage programmers to think about objects as of...

View Article

Answer by abarnert for Why use getters and setters/accessors?

You should use getters and setters when:You're dealing with something that is conceptually an attribute, but:Your language doesn't have properties (or some similar mechanism, like Tcl's variable...

View Article

Answer by GeZo for Why use getters and setters/accessors?

There is a good reason to consider using accessors is there is no property inheritance. See next example:public class TestPropertyOverride { public static class A { public int i = 0; public void add()...

View Article

Answer by Jonathan for Why use getters and setters/accessors?

If you want a readonly variable but don't want the client to have to change the way they access it, try this templated class:template<typename MemberOfWhichClass, typename primative> class...

View Article


Answer by MongooseLover for Why use getters and setters/accessors?

I can think of one reason why you wouldn't just want everything public.For instance, variable you never intended to use outside of the class could be accessed, even irdirectly via chain variable access...

View Article

Image may be NSFW.
Clik here to view.

Answer by Devrath for Why use getters and setters/accessors?

We use getters and setters:for reusabilityto perform validation in later stages of programmingGetter and setter methods are public interfaces to access private class members.Encapsulation mantraThe...

View Article


Answer by bobobobo for Why use getters and setters/accessors?

Code evolves. private is great for when you need data member protection. Eventually all classes should be sort of "miniprograms" that have a well-defined interface that you can't just screw with the...

View Article

Answer by R. Martinho Fernandes for Why use getters and setters/accessors?

A public field is not worse than a getter/setter pair that does nothing except returning the field and assigning to it. First, it's clear that (in most languages) there is no functional difference. Any...

View Article


Answer by Jorge Aguilar for Why use getters and setters/accessors?

Well i just want to add that even if sometimes they are necessary for the encapsulation and security of your variables/objects, if we want to code a real Object Oriented Program, then we need to STOP...

View Article

Answer by Mohamed for Why use getters and setters/accessors?

Don't use getters setters unless needed for your current delivery I.e. Don't think too much about what would happen in the future, if any thing to be changed its a change request in most of the...

View Article

Answer by andrers52 for Why use getters and setters/accessors?

From a object orientation design standpoint both alternatives can be damaging to the maintenance of the code by weakening the encapsulation of the classes. For a discussion you can look into this...

View Article

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 Article


Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article

Answer 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 Article



Answer 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 Article

Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article


Why 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
Browsing latest articles
Browse All 38 View Live




Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>
<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596344.js" async> </script>