Java Reference
In-Depth Information
Table 4.1
Sample JavaBean property names and methods
Property name/type
Get method
Set method
xcoordinate /Double public Double getXcoordinate() public void setXcoordinate
(Double newValue)
xCoordinate /Double public Double getxCoordinate() public void setxCoordinate
(Double newValue)
XCoordinate /Double public Double getXCoordinate() public void setXCoordinate
(Double newValue)
Xcoordinate /Double
Not allowed
Not allowed
student /Boolean
public Boolean getStudent()
public void setStudent(Boolean
newValue)
student /boolean
public boolean getStudent()
public boolean isStudent()
public void setStudent(boolean
newValue)
Indexed properties are properties that represent an array of values. For example,
if you have an Order bean, you may want an indexed property for the OrderItem
objects associated with that Order . According to the specification, the signatures
for the get and set methods for it would be
public void setOrderItem(OrderItem[] newArray);
public OrderItem[] getOrderItem();
public void setOrderItem(int index, OrderItem oi);
public OrderItem getOrderItem(int index);
In our experience, overloading bean properties (as the specification suggests, and
as demonstrated in the previous example of indexed properties) is not very well
supported by most tools and can often cause a great deal of confusion for devel-
opers. It is also not immediately clear by looking at the names what the difference
is between the two getOrderItem() methods. For that reason, we prefer to use the
following signatures instead:
public void setOrderItemArray(OrderItem[] newArray);
public OrderItem[] getOrderItemArray();
public void setOrderItem(int index, OrderItem oi);
public OrderItem getOrderItem(int index);
Here's another (nonstandard, but more functional) way to implement set methods:
public BeanType setProperty(PropType newValue){
this.property = newValue;
return this;
};
Search WWH ::




Custom Search