I l @ ve RuBoard |
A Bean is an encapsulated unit of data with accessor methods to set and get properties, and action methods to do things with the data. As a simple example, a user Bean might have properties such as lastName , firstName , city , state , and zipCode . It might also have action methods such as validateZipCode or saveToDatabase . The only real requirement for a Bean is that it implements a get and set method of each property that it contains. The methods must start with get or set . That's the basic definition of a Bean. Let's look at a simple Bean that implements user data in Listing 2.1. Listing 2.1 User.javapackage com.cartapp.user; public class User { protected String lastName; protected String firstName; public String getLastName() { return lastName; } public void setLastName(String lname) { lastName = lname; } public String getFirstName() { return firstName; } public void setFirstName(String fname) { firstName = fname; } } Obviously, a real user object would store a lot more information, as you'll see later in the chapter. However, this example lets you see how to access data in a Bean. You should always make the actual class variables holding the data protected because good encapsulation methodology indicates that all outside access should be through the methods. |
I l @ ve RuBoard |