Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Mutator method
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{Short description|Computer science method}} In [[computer science]], a '''mutator method''' is a [[method (computer science)|method]] used to control changes to a variable. They are also widely known as '''setter''' methods. Often a setter is accompanied by a '''getter''', which returns the value of the private member variable. They are also known collectively as '''accessors'''. The mutator method is most often used in [[object-oriented programming]], in keeping with the principle of [[encapsulation (computer science)|encapsulation]]. According to this principle, member [[variable (programming)|variable]]s of a [[class (computer science)|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 can be compared to [[assignment (computer science)|assignment]] [[operator overloading]] but they typically appear at different levels of the object hierarchy. Mutator methods may also be used in non-object-oriented environments. In this case, a [[Pointer (computer programming)|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 responsibility falls to the [[Software developer|developers]] to ensure the variable is only modified through the mutator method and not modified directly. In programming languages that support them, [[Property (programming)|properties]] offer a convenient alternative without giving up the utility of encapsulation. In the examples below, a fully implemented mutator method can also [[Data validation|validate]] the input data or take further action such as triggering an [[Event (computing)|event]].
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)