Mutator method

In the examples below, a fully implemented mutator method can also validate the input data or take further action such as triggering an event.

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.

Accessors conversely allow for synthesis of useful data representations from internal variables while keeping their structure encapsulated and hidden from outside modules.

A monetary getAmount accessor may build a string from a numeric variable with the number of decimal places defined by a hidden currency parameter.

These simplified accessors still retain the advantage of encapsulation over simple public instance variables, but it is common that, as system designs progress, the software is maintained and requirements change, the demands on the data become more sophisticated.

The benefit of automatically creating them in the early days of the implementation is that the public interface of the class remains identical whether or not greater sophistication is added, requiring no extensive refactoring if it is.

In the early days of an implementation, when there is little or no additional code in these blocks, it makes no difference if the private instance variable is accessed directly or not.

As validation, cross-validation, data integrity checks, preprocessing or other sophistication is added, subtle bugs may appear where some internal access makes use of the newer code while in other places it is bypassed.

[4] CLOS itself has no notion of properties, although the MetaObject Protocol extension specifies means to access a slot's reader and writer function names, including the ones generated with the :accessor option.

[6][7] A Student instance can be used like this: This is a simple class in Delphi language which illustrates the concept of public property for accessing a private field.

Defining individual accessor and mutator methods creates space for pre-processing or validation of the data Read-only simple public access to implied @name variable Read-write simple public access to implied @name variable This example illustrates the VB.NET idea of properties, which are used in classes.