File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

is and get

 
K Robert
Ranch Hand
Posts: 116
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 116
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But, if it's a getter method, doesn't it have to be named "get"?
 
Joel McNary
Bartender
Posts: 1824
Eclipse IDE Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Scott Selikoff
author
Saloon Keeper
Posts: 3901
16
Eclipse IDE Flex Google Web Toolkit
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
HE Jak
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Joel McNary,'is' is for boolean ,while 'get' is a method always used for get value.
 
K Robert
Ranch Hand
Posts: 116
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! This helped alot and the field in question "is" a boolean.

Thanks again!
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Stephen Boston
Ranch Hand
Posts: 165
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you need should as well. As in shouldCoverMouthWhenBurping();

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic