aspose file tools*
The moose likes Beginning Java and the fly likes What is an Accessor Method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is an Accessor Method?" Watch "What is an Accessor Method?" New topic
Author

What is an Accessor Method?

Thomas Young
Greenhorn

Joined: Jul 17, 2008
Posts: 29
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 29233
    
138

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Thomas Young
Greenhorn

Joined: Jul 17, 2008
Posts: 29
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

Joined: Apr 02, 2008
Posts: 813

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?


Regards, Sud.
SCJP 5 ScjpFAQ JLS
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
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

Joined: Oct 27, 2005
Posts: 19541
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is an Accessor Method?
 
Similar Threads
Type consistancies and code question
Nullpointer exception in servlet
JPA and static inner class, possible?
java:12: cannot find symbol
Calling a method from another class