Abbey Workshop

Java: Creating a Javabean

This is an example of a JavaBean that might be used in a typical JavaServer Page. The main purpose of the JavaBean specification was to allow developers to create graphical components that could be reused in GUI applications. However, they can also be used to create data oriented business logic components like this sales tax bean.

Requirements for a JavaBean

The only real requirement for a JavaBean is that it has a zero argument constructor. If no constructor is defined, then a zero argument constructor is assumed. For example:

//example 1
public class SalesTaxBean{
}
//example 2
public class SalesTaxBean{
  public SalesTaxBean(){
  }
}

Both these class definitions are perfectly legal. They don't do anything, but they are JavaBeans. In the first case, since no zero argument constructor is created, one is assumed. The second example shows the class with the zero argument constructor.

Now let's add some data to our object. For this simple bean, there are two variables, taxRate (the sales tax rate for this transaction) and amount (the about of the purchase for this transaction). Both variables are declared private, so access must be made through defined access methods.

getters and setters

Variables in a JavaBean normally have two methods associated with each variable, a get method and a set method. The get method, or getter, retrieves the value stored in the variable. The set method, or setter, sets the value for the variable. Though it is normal to have a get and a set method for each variable, it is not required. For example, a read only bean that has getters, but no setters. Also, a write only bean with setters, but no getters. Their are uses for either. However, commonly a bean has both as in our example. Here's the bean with its getters and setters.

SalesTaxBean.java (Click for Source)

   1:public class SalesTaxBean{
   2:    // Two Attributes
   3:    private double taxRate;
   4:    private double amount;
   5:
   6:    public SalesTaxBean(){ // Sample zero argument constructor
   7:        setTaxRate("0");
   8:        setAmount("0");
   9:    }
  10:
  11:    public double getTaxRate(){ // Tax Rate Getter
  12:        return taxRate;
  13:    }
  14:
  15:    public void setTaxRate(String newTaxRate){ // Tax Rate Setter
  16:        taxRate = Double.valueOf(newTaxRate).doubleValue()/100;
  17:    }
  18:
  19:    public double getAmount(){ // Getter for amount
  20:        return amount;
  21:    }
  22:
  23:    public void setAmount(String newAmount){ // Setter for amount
  24:        amount = Double.valueOf(newAmount).doubleValue();
  25:    }
  26:
  27:    public double getSalesTax(){ // Calculates the sales tax
  28:        return amount * taxRate;
  29:    }
  30:
  31:    public double getTotal(){ // Calculates a total including tax
  32:        return amount + (amount * taxRate);
  33:    }
  34:}

A few things to note. First notice the zero argument constructor. The tax rate and amount are set to zero using setters.

Thus the set method is used to set the variables in all instances. Notice that numerical values are passed as a string. This is because HTML form data is passed to a servlet or JSP as a string. We just leave the values string and convert them to what is needed inside the bean. For example, the setTax method takes a string and converts it to a double. See line 24.

Additional Methods

We are not limited to getters and setters, we can also add methods to perform logical functions on our object variables. For example, the amount of sales tax paid needs to be calculated. It also might be nice to know the total amount of the purchase including tax. So we could add the methods on lines 27 and 31.