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 boolean rules for JavaBeans Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "boolean rules for JavaBeans" Watch "boolean rules for JavaBeans" New topic

boolean rules for JavaBeans

Paul Duer
Ranch Hand

Joined: Oct 10, 2002
Posts: 98
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

Joined: Jul 18, 2001
Posts: 2712
Moving to Java in General (beginner).
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

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.

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Kathy Sierra
Cowgirl and Author

Joined: Oct 10, 2002
Posts: 1589
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.
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

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
I agree. Here's the link:
subject: boolean rules for JavaBeans
It's not a secret anymore!