Java Reference
In-Depth Information
What makes a bean?
Essentially, the JavaBean specification is a set of rules for defining components for
use with Java. We have those rules to make it possible for tool creators (like the
people who work on i BATIS ) to know how to interact with the components that we
use in our applications. Think of the specification as a middle-ground common
language for framework developers and application developers.
The only rules in the specification that apply to i BATIS are the ones that con-
cern property naming. Property names are defined in a JavaBean by a pair of
methods that the specification refers to as accessor methods . Here is the pattern for
creating accessor methods for a property named value :
public void setValue(ValueType newValue);
public ValueType getValue();
These accessor methods define a simple property named value with a lowercase v .
Java bean properties should always start with a lowercase letter, with very few
exceptions. The types for the two methods should always be the same. If you have
a setter that accepts a Long, and a getter that returns an Integer, you will have
problems. Always make them the same.
Property names with multiple words are named using a pattern known as
camel case (or camelCase), which means that uppercase letters are used to sepa-
rate the words:
public void setSomeValue(ValueType newValue);
public ValueType getSomeValue();
When creating properties for JavaBeans, it's important to remember that abbrevi-
ations are generally treated as words, not individual letters. For example, the
abbreviation URL would become a property named url , and the methods getUrl
and setUrl would be used to access that property.
Another oddity in the specification is that property names with the second let-
ter capitalized are treated differently. If the second letter of the property name is
uppercase, then the name after the get or set part of the method for that property
is used as the property name, and the case is left unchanged. We will clarify this
confusing rule in table 4.1 (which also shows get and set methods for the proper-
ties listed).
Properties of type boolean (the primitive) are allowed to use isProperty for
the getter, but if the type is Boolean (the object, or boxed version), then the stan-
dard getProperty name is required instead.
Search WWH ::




Custom Search