This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is an Accessor Method?

 
Thomas Young
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is an Accessor Method just a method which returns information about an object?
For example, If I have the following class:

package access;
class cup {

private String status = "The Cup Exists";
private boolean working = true;



Would "getStatus" and only getStatus be an accessor method, or would all ("fill", "empty", "smash", "getStatus") be an accessor methods?
My understanding is the latter and that there are "setter" and "getter" methods which are "Accessor Methods".

Could anyone please confirm/reject my understanding, and additionally, provide any more information/examples.

Regards,
TY.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34839
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas,
I think only getStatus is an accessor method. Accessors must be read only in that they only "get" state and do not set/mutate it.
 
Thomas Young
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you could be correct, as I was reading the following wikipedia page which suggests the following:
(Mutator Method - Wikipedia)

An Accessor = a "getter".

A Mutator = a "setter".

Any comments/suggestions/thoughts?

-Update
..and I have just found this:
"A method that changes the state of an object is no longer called an accessor method, but rather an update method, a modifier method, or a mutator method. Objects that provide such methods are considered mutable objects."
Methods (computer science) - Wikipedia
[ December 23, 2008: Message edited by: Thomas Young ]
 
sudipto shekhar
Ranch Hand
Posts: 826
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The prefix get states a lot... stating access the values of this class through this getValue() method.
The designers of OO language were great... what say?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49776
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right that the getStatus method should return the status field, which is what you call an accessor method. You have got all sorts of type inconsistencies in that code snippet; it will return all sorts of compiler errors.
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeanne Boyarsky:
Thomas,
I think only getStatus is an accessor method. Accessors must be read only in that they only "get" state and do not set/mutate it.

As an addendum, accessor methods usually start with either "is" (for booleans only) or "get". For example, getValue(), isReadOnly(), etc. Mutator methods usually start with "set".

This naming convention is called the JavaBean naming convention.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic