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
0
votes
3 answers

Can't get setter to work

The code: HEADER class Parser{ private: unsigned int cant_documentos; unsigned int cant_terminos; std::map dicc_stopwords; std::map hash_frecuencias_globales; …
user3013172
  • 1,637
  • 3
  • 15
  • 26
0
votes
1 answer

Redefine Object.defineProperty in Javascript

I would like to learn if it is possible to redefine the "definePropery" function of the Object(.prototype) in a subclass(.prototype) so that the setter not only sets the value of the property but also eg executes an additional method. I have tried…
kwicher
  • 2,092
  • 1
  • 19
  • 28
0
votes
3 answers

What's wrong with this? Setters/Getters (edited ) header files included

when I have the following member in a class employee headOfDepartment; what's wrong these setters and getters? void department::setHeadOfDepartment( employee depEmployee) { headOfDepartment=depEmployee; } employee…
user2949483
  • 103
  • 1
  • 1
  • 8
0
votes
1 answer

Doctrine : setter that gets other values from same entity

I'm trying to build an Entity with a "xml" text field that must insert in the database the concatenation of other fields from the same entity. It looks like this (without annotations) : class Category { private $id; private $title; private…
user2094540
0
votes
1 answer

How to set a DepedencyProperty by a style setter?

My user control have the following DP: public static readonly DependencyProperty ButtonAnimationColorProperty = DependencyProperty.Register("ButtonAnimationColor", typeof(Color), typeof(MyControl), new…
Guilherme
  • 5,143
  • 5
  • 39
  • 60
0
votes
1 answer

Getter and setter methods and reading a txt file

Esentially, I'm reading a text file in my driver class, but I'm calling a methods from another class with instructions to read from the text file from another class, but it just isn't working. This is what I have so far in my driver class: import…
user2120893
  • 123
  • 1
  • 3
  • 12
0
votes
2 answers

How do I use Java getters and setters with a collection of data without explicitly typing out the attributes for each item?

I am very new to Java and to programming in general, and I have an assessment to complete where I load employees (with name, age, and department attributes; department can be only one of four enumerated values) into a program that will sort them by…
0
votes
0 answers

JavaScript: dynamic object cross-reference pool

Some question about ability of writing hybrid getters/setters... For my chess app I have an object diagram much more similar to a graph than a tree. So I decide to pool the various kind of objects in a data structure holding both object instances…
0
votes
0 answers

Do I still use Getters and Setters when using static methods

Well when i use my getter and setter from a static method from another class everything is okay but it gives a warning that it should be used in a static context. So when I dont use any getters or setters it takes away that warning but what is still…
0
votes
1 answer

Needs help assigning 'parsed' values to setter

/****Setters and Getter****/ public double getx1() { return x1;//x1 getter } public void setx1(double x1) { this.x1 = x1;//x1 setter } public double getx2() { return x2;//x2 getter } public void setx2(double x2) { this.x2 = x2;//x2…
John
  • 3
  • 2
0
votes
0 answers

Does java spring recognize method signature?

Code: myProp; and 2 setters: public void setMyProp(String ...) and public void setMyProp(List ...) Spring container always use method…
user710818
  • 23,228
  • 58
  • 149
  • 207
0
votes
1 answer

Java Getter method getting after the first get but not the second

So i am re writing some already working code using encapsulation(Im a tafe student) and the method is getting in the first methode but not in the second First Methode: public void getDetails() throws IOException { do { String…
Callum
  • 1
0
votes
2 answers

Dynamically added glow effects to UIElement using triggers

Im new to wpf, and looking for good tutorials to help understand triggers better but im not having much luck. So i thought I would seek some help here. Here is what im trying to do, i have a ScrollViewer that has a stack panel, in the code behind I…
nagates
  • 620
  • 13
  • 40
0
votes
3 answers

Setters, why won't this work?

I've been toying for hours, changing static, private, public etcetera :) But it still won't work. If I change static at one place, I get an error at another place etc. I have a class called person. I've used NON-static Setters because the Person()…
swennemen
  • 945
  • 1
  • 14
  • 24
0
votes
1 answer

AS3: setters and vectors

I have a setter for a vector that works properly, but I'm trying to make it do some additional things when one of the elements is changed. private var _myVect:Vector. = new [0,0,0,0]; public function set myVect(newVect:Vector.):void…
BladePoint
  • 632
  • 5
  • 16
1 2 3
99
100