Questions tagged [setter]

Setter is public mutator method, used in object-oriented programming, which gives new value to a private member of a class.

In computer science, a mutator method is a method used to control changes to a variable. They are also widely known as "setter" methods. Often a setter is accompanied by a "getter" (also known as an accessor), which returns the value of the private member variable.

The mutator method, sometimes called a "setter", is most often used in object-oriented programming, in keeping with the principle of encapsulation. According to this principle, member variables of a class are made private to hide and protect them from other code, and can only be modified by a public member function (the mutator method), which takes the desired new value as a parameter, optionally validates it, and modifies the private member variable.

Mutator methods may also be used in non-object-oriented environments. In this case, a reference to the variable to be modified is passed to the mutator, along with the new value. In this scenario, the compiler cannot restrict code from bypassing the mutator method and changing the variable directly. The onus falls to the developers to ensure the variable is only modified through the mutator method and not modified directly.

In programming languages that support them, properties offer a convenient alternative without giving up the utility of encapsulation.

The alternative to defining mutator and accessor methods, or property blocks, is to give the instance variable some visibility other than private and access it directly from outside the objects. Much finer control of access rights can be defined using mutators and accessors. For example, a parameter may be made read-only simply by defining an accessor but not a mutator. The visibility of the two methods may be different; it is often useful for the accessor to be public while the mutator remains protected, package-private or internal.

The block where the mutator is defined provides an opportunity for validation or preprocessing of incoming data. If all external access is guaranteed to come through the mutator, then these steps cannot be bypassed. For example, if a date is represented by separate private year, month and day variables, then incoming dates can be split by the setDate mutator while for consistency the same private instance variables are accessed by setYear and setMonth. In all cases month values outside of 1 - 12 can be rejected by the same code.

Source:http://en.wikipedia.org/wiki/Mutator_method

1760 questions
2309
votes
23 answers

What is the best way to give a C# auto-property an initial value?

How do you give a C# auto-property an initial value? I either use the constructor, or revert to the old syntax. Using the Constructor: class Person { public Person() { Name = "Initial Name"; } public string Name { get;…
bentford
  • 33,038
  • 7
  • 61
  • 57
1844
votes
37 answers

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 get/set, I can figure this one out very quickly,…
Dean J
  • 39,360
  • 16
  • 67
  • 93
259
votes
16 answers

How can we generate getters and setters in Visual Studio?

By "generate", I mean auto-generation of the code necessary for a particular selected (set of) variable(s). But any more explicit explication or comment on good practice is welcome.
Paul
  • 2,813
  • 2
  • 19
  • 12
239
votes
16 answers

Are getters and setters poor design? Contradictory advice seen

I'm currently working on a simple game in Java with several different modes. I've extended a main Game class to put the main logic within the other classes. Despite this, the main game class is still pretty hefty. After taking a quick look at my…
Mike B
  • 12,768
  • 20
  • 83
  • 109
203
votes
5 answers

What is the right way to override a setter method in Ruby on Rails?

I am using Ruby on Rails 3.2.2 and I would like to know if the following is a "proper"/"correct"/"sure" way to override a setter method for a my class attribute. attr_accessible :attribute_name def attribute_name=(value) ... # Some custom…
Backo
  • 18,291
  • 27
  • 103
  • 170
142
votes
14 answers

Getters \ setters for dummies

I've been trying to get my head around getters and setters and its not sinking in. I've read JavaScript Getters and Setters and Defining Getters and Setters and just not getting it. Can someone clearly state: What a getter and setter are meant to…
oksf
140
votes
14 answers

Simple Getter/Setter comments

What convention do you use to comment getters and setters? This is something I've wondered for quite some time, for instance: /** * (1a) what do you put here? * @param salary (1b) what do you put here? */ public void setSalary(float…
ThaDon
  • 7,826
  • 9
  • 52
  • 84
112
votes
6 answers

How do getters and setters work?

I'm from the php world. Could you explain what getters and setters are and could give you some examples?
ajsie
  • 77,632
  • 106
  • 276
  • 381
111
votes
4 answers

What are getters and setters for in ECMAScript 6 classes?

I am confused as to what the point of getters and setters are in ECMAScript 6 classes. What is the purpose? Below is an example I am referring to: class Employee { constructor(name) { this._name = name; } doWork() { …
TruMan1
  • 33,665
  • 59
  • 184
  • 335
104
votes
3 answers

Private setter typescript?

Is there a way to have a private setter for a property in TypeScript? class Test { private _prop: string; public get prop() : string { return this._prop; } private set prop(val: string) { //can put…
sheamus
  • 3,001
  • 4
  • 31
  • 54
86
votes
3 answers

Difference between @interface definition in .h and .m file

Normally we use @interface interface_name : parent_class { ...... } @end method in .h file and in .m file we synthesis the properties of variables declared in .h file. But in some code, this @interface.....@end method is kept in the .m…
Rajkanth
86
votes
15 answers

Public Data members vs Getters, Setters

I am currently working in Qt and so C++. I am having classes that has private data members and public member functions. I have public getters and setters for the data members available in the class. Now my question is, if we have getters and…
liaK
  • 11,422
  • 11
  • 48
  • 73
85
votes
10 answers

Conventions for accessor methods (getters and setters) in C++

Several questions about accessor methods in C++ have been asked on SO, but none was able satisfy my curiosity on the issue. I try to avoid accessors whenever possible, because, like Stroustrup and other famous programmers, I consider a class with…
Noarth
  • 3,981
  • 6
  • 23
  • 16
85
votes
12 answers

What is the point of getters and setters?

Possible Duplicate: Why use getters and setters? I have read books on Java, saying that it is good to create setters and getters for variables such as x and y. For example: public int getX(){ return x; } public void setX(int x){ this.x…
Anonymous181
  • 1,863
  • 6
  • 24
  • 27
71
votes
6 answers

Custom setter methods in Core-Data

I need to write a custom setter method for a field (we'll call it foo) in my subclass of NSManagedObject. foo is defined in the data model and Xcode has autogenerated @property and @dynamic fields in the .h and .m files respectively. If I write my…
Andrew Ebling
  • 10,175
  • 10
  • 58
  • 75
1
2 3
99 100