| FazBrowse Wikipedia Viewer | Main Page | | |
| Viewing: https://en.wikipedia.org/wiki/Mutator_method#Java_example |
| Contents |
|---|
| C |
| C# |
| Delphi |
| Java |
| ActionScript 3.0 |
| PHP |
| Python |
| Ruby |
| Rust |
| Smalltalk |
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, 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. 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 can be compared to 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 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 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.
In the examples below, a fully implemented mutator method can also validate the input data or take further action such as triggering an event.
.code student_get_ageprocobject:DWORD movebx,object moveax,student.age[ebx] ret student_get_ageendp student_set_ageprocobject:DWORD,age:DWORD movebx,object moveax,age movstudent.age[ebx],eax ret student_set_ageendpIn file Student.h:
In file importstd; usingstd::string; classStudent{ private: stringname; public: explicitStudent(conststring&name): name{name}{} [[nodiscard]] conststring&getName()constnoexcept{ returnname; } voidsetName(conststring&name)noexcept{ this->name=name; } };
This example illustrates the C# idea of properties, which are a special type of class member. Unlike Java, no explicit methods are defined; a public 'property' contains the logic to handle the actions. Note use of the built-in (undeclared) variable value.
In later C# versions (.NET Framework 3.5 and above), this example may be abbreviated as follows, without declaring the private variable name.
This is a simple class in Delphi language which illustrates the concept of public property for accessing a private field.
In this example of a simple class representing a student with only the name stored, one can see the variable name is private, i.e. only visible from the Student class, and the "setter" and "getter" are public, namely the "getName()" and "setName(name)" methods.
Or (using prototypes for inheritance and ES6 accessor syntax):
And starting with OS X 10.8 and iOS 6, while using Xcode 4.4 and up, syntax can be even simplified:
Or, using the Moose Object System:
PHP defines the "magic methods" __getand__set for properties of objects.
In this example of a simple class representing a student with only the name stored, one can see the variable name is private, i.e. only visible from the Student class, and the "setter" and "getter" is public, namely the getName() and setName('name') methods.
This example uses a Python class with one variable, a getter, and a setter.
In Ruby, individual accessor and mutator methods may be defined, or the metaprogramming constructs attr_reader or attr_accessor may be used both to declare a private variable in a class and to provide either read-only or read-write public access to it respectively.
Defining individual accessor and mutator methods creates space for pre-processing or validation of the data
In VB.NET 2010, Auto Implemented properties can be utilized to create a property without having to use the Get and Set syntax. Note that a hidden variable is created by the compiler, called _name, to correspond with the Property name. Using another variable within the class named _name would result in an error. Privileged access to the underlying variable is available from within the class.
Tim Lee (1998-07-13). "Run Time Efficiency of Accessor Functions".| View original on Wikipedia | Back | FazBrowse Home |