I've been having an argument about the usage of the word "accessor" (the context is Java programming). I tend to think of accessors as implicitly being "property accessors" -- that is, the term implies that it's more or less there to provide direct access to the object's internal state. The other party insists that any method that touches the object's state in any way is an accessor.
I know you guys can't win the argument for me, but I'm curious to know how you would define the term. :)
Accessor methods are methods that allow other clients (objects, classes, and users) to access the data of an object. Since we want other clients to access this, accessor methods are made public. There are two types of accessor methods: Getter Methods.
The accessor methods are publicly declared and return the property of the object. They return the value of a private field. The type of data returned depends on the type of the private field. In the above example, the class's getRollNumber() function is the accessor method.
accessor (plural accessors) Someone or something that accesses. (object-oriented programming) A function that retrieves a value, usually without changing any data.
An accessor is a class method used to read data members, while a mutator is a class method used to change data members.
By accessors, I tend to think of getters and setters.
By insisting that all methods that touch the object's internal state are accessors, it seems that any instance method that actually uses the state of the object would be an accessor, and that just doesn't seem right. What kind of instance method won't use the state of the object? In other words, an instance method that doesn't use the object's state in some way shouldn't be an instance method to begin with -- it should be a class method.
For example, should the BigDecimal.add
method be considered an accessor? It is a method that will read the value of the instance that the add
method was called on, then return the result after adding the value of another BigInteger
. It seems fairly straight forward that the add
instance method is not a getter nor a setter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With