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

Download source for: SalesTaxBean.java

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.

Google
  Web abbeyworkshop.com   

Copyright © Abbey Workshop 2006

Music Only Search
The Black MP3 Album
The MP3 Album Lyrics
Wprld Music Plus
Mp3 Songs Blog
Music Search Store
First Music Search
Mega MP3 Blog
E MP3 BLog
My MP3 Diary
Fast MP3 Finder
My Blog MP3
Mp3 Songs Mart
My MP3 Songs Album
Top Music Group
Mp3 Tune Records
Song Album Guide
Music DataDite
MP3 Buying Guide
Top MP3 Downloads
MP3 Tune Festival
FAST MP3 Downloads
MP3 Mart Online
Upper MP3
Premium MP3 Site
Music Data online
Fine MP3 Audio
Cheap Music Albums
Fine mp3 downloads
World MP3 Music Records
Mp3 Music Data Direct
The Fine Mp3
MP3 Files World
Home Music Store
DatMP3
Fat MP3 Audio
MP3 ZigZag
MP3 Songs Direct
MP3 Tune World
E MP3 Log
Boom MP3 Music
Speedy MP3
Top 20 MP3 music
Deep MP3 World
Deep MP3 Download
Best-20 MP3
Top Twenty MP3
Deep MP3
Big MP3 Guide
Deep MP3 Audio
Mp3 R.O.B.
MP3-Pop Online
Greate-20-MP3
MP3 Appear
3T-MP3