This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "is and get" Watch "is and get" New topic
Author

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: 2367
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
Bartender

Joined: Aug 20, 2001
Posts: 1817

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: 3700
    
    5

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.


My Blog: Down Home Country Coding with Scott Selikoff
HE Jak
Greenhorn

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: http://aspose.com/file-tools
 
subject: is and get
 
Similar Threads
Most Stupid thing your ever did
Cookbook
life is boring
Tech Word Game
[GROSS] Necrophilia