File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes is and get Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "is and get" Watch "is and get" New topic

is and get

K Robert
Ranch Hand

Joined: May 16, 2003
Posts: 116
Is there a difference between "is" and "get" for a getter method? I've seen both and am not sure which to use. For example: is isName() the same as getName()?

Thanks in advance.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2398
Well the name you give a method is really irrelevant as long as it's not a method you've inherited. The implementation of the method is what is important.
K Robert
Ranch Hand

Joined: May 16, 2003
Posts: 116
But, if it's a getter method, doesn't it have to be named "get"?
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

It's not totally irrelevant. In order to adhere to the JavaBeans spec, an attribute named myAttribute should have a getter named getMyAttribute and a setter named setMyAttribute. This way Bean boxes can infer the getter and setters.

The exception is boolean fields. A boolean field named myBoolean still has a setter of setMyBoolean, but the getter can be isMyBoolean.

I, myself, tend to include the "is" as part of the field name and use the "get" for the getter. So if a boolean checked to see if something was printed or not would be:

while others would have

Either way workd for JavaBeans, though.

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3753

On a related note, any one else besides me wish they'd get rid of is() getters for booleans?

I know for some web service tools, IBM's WSAD/RAD/WAS/RAS in particular, where you convert from an EJB to a Web service directly it is mandated that all getters use get() format except booleans which must use is() format. I'm not sure if this quirk is unique to IBM's tool, but I also use other tools which mandate get() usage accross the board, meaning there are places where manual conversions between is() and get() are required for all booleans.

I'm not sure how many other people have encountered this but I find it very frustrating. Just wish they'd stick to get() for everything.

[OCA 8 Book] [Blog]
HE Jak

Joined: Oct 26, 2005
Posts: 2
I agree with Joel McNary,'is' is for boolean ,while 'get' is a method always used for get value.
K Robert
Ranch Hand

Joined: May 16, 2003
Posts: 116
Thanks! This helped alot and the field in question "is" a boolean.

Thanks again!
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
The Java library has been known to use other words, too. hasNext() comes to mind. I'd have to look but I bet I've used "can" somewhere like canBurpLoudly() (sorry, just finishing a warm diet soda.)

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Stephen Boston
Ranch Hand

Joined: Jul 14, 2005
Posts: 165
Maybe you need should as well. As in shouldCoverMouthWhenBurping();

Steve<br /> <br />No matter where you go, there you are.<br /> <br />"My evil self is at the door, and I have no power to stop it."
I agree. Here's the link:
subject: is and get
It's not a secret anymore!