Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the definition of "accessor method"?

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. :)

like image 886
Rytmis Avatar asked Mar 08 '09 14:03

Rytmis


People also ask

What are accessor methods?

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.

What is an accessor method give an example?

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.

What is the meaning of accessor?

accessor (plural accessors) Someone or something that accesses. (object-oriented programming) A function that retrieves a value, usually without changing any data.

What are accessor methods and mutator methods?

An accessor is a class method used to read data members, while a mutator is a class method used to change data members.


1 Answers

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.

like image 159
coobird Avatar answered Oct 16 '22 08:10

coobird