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

Please explain Getter and Setters in Objective C

Possible Duplicate: Setters and Getters (Noobie) - iPhone SDK I am a beginner here. I have just started learning iOS for the last two months and I do not have any programming background. (Little bit of Java though). Can anyone please explain what…
GenieWanted
  • 4,473
  • 4
  • 24
  • 35
32
votes
3 answers

AutoMapper mapping properties with private setters

Is it possible to assign properties with private setters using AutoMapper?
leozilla
  • 1,296
  • 2
  • 19
  • 29
31
votes
4 answers

'this' argument has type const but function is not marked const

Okay so I'm a bit of a noob at C++ and in my second assignment I am required to make classes with public and private arguments etc, etc. Basically the mutator functions won't work because apparently they're not of type const? This is the header file…
Liam George
  • 343
  • 1
  • 3
  • 5
31
votes
2 answers

Swift - Custom setter on property

I am converting a project in to Swift code and have come across an issue in a setter. My Objective-C code looked like this: - (void)setDocument:(MyDocument *)document { if (![_document isEqual:document]) { _document = document; …
Adam Carter
  • 4,741
  • 5
  • 42
  • 103
29
votes
11 answers

Java :Setter Getter and constructor

I'm a bit confused about the use of getter/setters and constructors (see the below code for an example) public class ExampleClass { private int value = 0; public ExampleClass () { value = 0; } …
user2088260
28
votes
3 answers

Monitor All JavaScript Object Properties (magic getters and setters)

How do I emulate PHP-style __get() and __set() magic getter/setters in JavaScript? A lot of people say that this is currently impossible. I am almost certain that it is possible because projects like nowjs (http://nowjs.com) do something like…
BMiner
  • 16,669
  • 12
  • 53
  • 53
27
votes
8 answers

Is there a way to automatically generate getters and setters if they aren't present in C++?

I'm experienced with Objective-C, and in Objective-C you can let the compiler generate getters and setters for you if they aren't already present (@synthesize). Is there a way to do this in C++, or do I need to implement all getters and setters…
user142019
26
votes
7 answers

Why to use __setattr__ in python?

I don't know for why using __setattr__ instead simple referencing like x.a=1. I understand this example: class Rectangle: def __init__(self): self.width = 0 self.height =…
user278618
  • 19,306
  • 42
  • 126
  • 196
26
votes
4 answers

Get Getter Function in Javascript

In JavaScript there is the possibility to create getters and setters the following way: function MyClass(){ var MyField; this.__defineGetter__("MyField",function(){ return MyField; }); this.__defineSetter__("MyField",function(value){ MyField…
Van Coding
  • 24,244
  • 24
  • 88
  • 132
26
votes
2 answers

protobuf-net - generated class from .proto - Is repeated field supposed to be Read Only with no setter?

I am totally confused about this. I have looked around and can't seem to find a direct answer. I have a .proto file that my project, which has all been java, uses to create some messages. There is a repeated Info field. Which is a type we created.…
Mimerr
  • 390
  • 1
  • 5
  • 14
25
votes
1 answer

Why C# compiler does not allows private property setters in interfaces?

In certain scenario like a MVVM view-model, I sometimes needs to have private setter as the view-model exposes a state that can only be modified internally. So is this wrong to need a private setter on an interface? (and I mean not particularly in…
Ucodia
  • 7,410
  • 11
  • 47
  • 81
25
votes
8 answers

Shortcut for calling all the setter methods on an object in Eclipse?

I want to create an object of a class in and then set all the properties in it using setter methods. There are more than 150 setter methods and I do want to type each one of them or even type in the object instance name Object instance type in dot .…
simranNarula
  • 641
  • 3
  • 9
  • 14
24
votes
9 answers

Constructor with all class properties or default constructor with setters?

Following are the two approaches: constructor with all the class properties Pros: I have to put an exact number of types of parameters so if I make an error the compiler warns me (by the way, is there a way to prevent the problem of having…
Alberto Zaccagni
  • 30,779
  • 11
  • 72
  • 106
24
votes
1 answer

MutableStateflow Value Vs Update vs Emit

Let say I have a MutableStateFlow variable. What is the main differences and usage of the three cases mutable.value = 1 mutable.emit(2) mutable.update {3}
T D Nguyen
  • 7,054
  • 4
  • 51
  • 71
24
votes
1 answer

C# Can a base class property be invoked from derived class

I have a base class with a property which has a setter method. Is there a way to invoke the setter in the base class from a derived class and add some more functionality to it just like we do with overriden methods using the base keyword. Sorry I…
Shahid
  • 995
  • 3
  • 11
  • 24