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

boolean rules for JavaBeans

 
Paul Duer
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
Can somebody refresh me on the rules for boolean values in JavaBeans? Do they always use isMyBooleanValue notation and then have a get method starting with is and a set starting with setIs?
I like to use booleans to setup static pieces of my SQL statements to either be on or off. Is this a safe pratice? Is it good OO to use lots of gets and sets for SQL statement pieces?
 
Chris Mathews
Ranch Hand
Posts: 2712
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Java in General (beginner).
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rules for booleans:
variable name: active
getter: isActive
setter: setActive
-OR- (This also works, at least in NetBeans)
variable name: isActive
getter: getIsActive
setter: setIsActive
(I personally (and purposefully) flout the official naming conventions and use the latter form, as it makes my variable names more meaningful and obvious.)
As far as answering your SQL question, I don't know...I don't use static SQL statements--mine are built dynamically.
 
Kathy Sierra
Cowgirl and Author
Rancher
Posts: 1589
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy,
I just wanted to clarify that according to the JavaBeans specification, both ways are fine:
property name: active
getter: isActive or getActive
setter: setActive
So the only difference is in the getter, and EITHER one is correct in the specification. So any true JavaBeans-compliant tool will work with either as part of a property. Remember, though, that it makes NO difference what you actually name the variable. You might not even *have* a single variable for a property. A property can be a "virtual" instance variable -- something that exists ONLY because there is a getter/setter for it
And personally, I strongly prefer to use the naming convention, because if you anyone ends up using your class as a reusable component, and they want to use a high-level tool, it will make all the difference in the world that you followed it.
cheers,
Kathy
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whew--
Good to know that I'm not going too far from standard, if at all. I do have conventions--they were just in opposition to the "official" conventions that I came across (which stated that boolean getters should always start with isXXX, but since my variables were named isXXX, isIsXXX didn't make much sense.
And since my components work in my beanbox, I assume I'm doing something right
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic