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

First time using methods and constructors in JAVA, recieving errors in a program to determine raises

I'm coding a fairly simple program that simply outputs 2 employee's Names, their salary, and a 10% raise to that salary. I have 2 issues: 1) The salary prints as '$0.000000' 2) Ii cannot get the raise method to work properly Here's my code: public…
doobiz
  • 13
  • 2
  • 5
0
votes
1 answer

JSP + JAVA + Oracle sample application

I am trying to create a new application in JSP, using the jsp:useBean to call the class file. I am very successful in retrieving the data from Oracle and send it to the JSP file as an arraylist. Now I am trying to send the result using a class, and…
Anandkumar
  • 1,338
  • 13
  • 15
0
votes
0 answers

jsp not setting fields in bean on page reload

I have a simple jsp with two input fields and when the user gets to the page and enters data into the fields I want a bean to be populated when they click submit. I want the info from the jsp to be populated in a bean with a set. On the jsp I use…
0
votes
2 answers

how to use vector to define array of objects of one class into other class in different file

I want to use array of objects(in data.h file) to my user.h file .i tried this thing as below but my try is not succeed .i want help from you guys .what is wrong in my script. please correct me // data.h file class data { private: int…
user3213849
  • 11
  • 1
  • 1
  • 6
0
votes
1 answer

Exercise 115; should I use getters/setters or something else?

I took care of exercise 105. I don't know what to do with 115 though. I've worked a little bit more and progressed a slight bit, but here's the exercise: A team of biologists is conducting an experiment that involves collecting data on animals…
0
votes
2 answers

Getters and setters for UIView

In my UIView subclass' .h: @interface MyView : UIView @property (nonatomic, strong) UIImageView *imageView; @end I'm setting up the UIImageView on my UIView subclass like this (init method): self.imageView = [[UIImageView…
Oscar Swanros
  • 19,767
  • 5
  • 32
  • 48
0
votes
2 answers

Boolean getter function not working

I have this simple class with a bool and a getter/setter for it: tile.h class Tile { public: Tile(); virtual ~Tile(); bool isActive() const { return isActive_; }; void setActive(bool status) { isActive_ = status; }; private: bool…
A. D.
  • 728
  • 1
  • 9
  • 27
0
votes
0 answers

GWT RequestFactory

Using GWT 2.5.1, i've got the following error : com.google.web.bindery.requestfactory.server.UnexpectedException: Could not locate setter for property cptdossier in type org.nit.persistance.entites.Tllog I have the feeling i misunderstood how to…
Tanc
  • 667
  • 3
  • 6
  • 25
0
votes
4 answers

VB.net Setter By Component Name

I don't do a bunch of work in VB.net, however I'd like to do some things that I do in other languages. I have a form that has a bunch of rows of text boxes. Instead of creating setters for each text box of each row, I wish to have a dynamic setter,…
James Oravec
  • 19,579
  • 27
  • 94
  • 160
0
votes
1 answer

ExtJS 4.2.1 - getters/setters using Associations not working

I'm having trouble trying to invoke getters/setters on a Model object that has an association with one other model. Here are the classes: Category.js Ext.define('Chapter05.model.Category', { extend: 'Ext.data.Model', fields: [ {…
Android Noob
  • 3,271
  • 4
  • 34
  • 60
0
votes
2 answers

Initialization through XML of autopropery with private setter in Autofac

I'm using some Autofac modules to initialize config files. E.g.: public class Config() { public String ConnectionString { get; set; } } Regarding XML configuration everything looks like:
Johnny_D
  • 4,592
  • 3
  • 33
  • 63
0
votes
1 answer

c# access form element from another class

I have a List View element listViewMedia in one class and want to update it with data from another file (Strings, basically) so, i have created a public method public void addToListViewMedia() { listViewMedia.Items.Add(new…
joko
  • 182
  • 2
  • 11
0
votes
1 answer

Overriding setters for assign, retain and copy

I am a newbie in Objective-C and would like to know more about the non-arc based programming especially to override the setters for assign, retain and copy. Could someone please help me out. And also please brief on the process.
0
votes
3 answers

Get the value which I set before

I have a little problem with my program, that includes 2 classes. I want to use the Text of my jTextField1 at this moment when the user clicks on the New-button of my program. Than I want to use this value in the second class to write a simple…
Pulsar
  • 13
  • 2
0
votes
4 answers

How to use Getter setters in array

I have been using the c++ and new in c#. In c++ i use int a[8] to declare an array and if the array is of an object we set the value by item[0].SetID(5) which will set the value of first item's ID to 5. But i m unable to do it in c#. namespace…