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
2 answers

computing the total cost of bookings

id appreciate anyones help here. Below is my class that contains all my setters and getters, in my main class, ive created 3 customers and in the value parameters, i have 3 different numbers. What i need to do is find the total value of all of those…
user2757842
  • 651
  • 1
  • 11
  • 24
0
votes
2 answers

iOS pass NSArray in setter method possible

I try to write a custom setter method but I fail with the error message "Type of property 'pValue' does not match type of accessor 'setPValue:'". The array is passed successfully but I don't understand the the error message. my .h-file: @property…
JFS
  • 2,992
  • 3
  • 37
  • 48
0
votes
3 answers

Is it good practice to include a setter in my object even if I know I will never need it

An interesting question came up today. Is it good practice to great a setter for a field that I know I will never need to re-set after the constructor? P.S. I, of course, understand the reasons for using a private field and a getter vs. a public…
Matthew Salsamendi
  • 294
  • 1
  • 5
  • 14
0
votes
2 answers

Unexpected behavior using getters and setters

Look this code: