A getter is a public accessor method, used in object-oriented programming, which returns the value associated with a private member of a class.


Setter is public mutator method, used in object-oriented programming, which gives new value to a private member of a class.


Quality Example

"If the calculation is expensive or the getter is executed more often than the setter for the changeable properties playername by far it can be an optimization to change the property inside the class into a read-write property and set the value every time the changeable properties are changed"

from question "Updating a class with readonly properties in Objective-C"

"If your getter takes more than one argument it s not called a setter anymore"

from question "How does the compiler know how many arguments to generate while using @property?"

"Note that the test bean has a very special design as the getter returns a more general type number than the setter requests integer"

from question "Introspector.getBeanInfo, setters with property subclasses and Java 8"


"But then your setter is less restricted than your getter which is odd."

from question "Why is internal protected not more restrictive than internal?"

Far more

"The code in the setter is far more interesting than the getter should it not take precedence and be defined first"

from question "Why define the getter before the setter (coding convention)"


"The setter complexity can be higher than the getter and thus validate a unit-test"

from question "Should unit tests be written for getter and setters?"

More efficient

"If we put our filter in between these calls either in getter or setter setter is more efficient as it is called only when filters change we modify original filtered list with our filter and return it back through the getter"

from question "Primefaces custom filter in datatable"


"You can override the setter s level to be lower than the getter s level with either private set or internal set"

from question "Can `public private(set) var numberOfEdits = 0` be written as `public(get) private(set) var numberOfEdits = 0`?"

Back to Home
Data comes from Stack Exchange with CC-BY-SA-3.0