You can call a JavaBean a Bean and everyone will know what you’re talking about, as long as you’re not discussing coffee. The JavaBean documentation refers to the rules as design patterns. However, this term is more generally used to refer to design patterns such as the Model-View-Controller design pattern. Naming conventions is a more appropriate term.
As an example of the special Bean rules, let’s look at properties. A Bean’s properties that are exposed (public) are available only through the getter and setter methods, because the actual property definition is typically private (available to only the defining class). The properties follow the naming convention that the first letter of the property must be lowercase and any subsequent word in the name should start with a capital letter, such as mailingAddress. (We explain getters and setters after Listing 1-2.) Listing 1-2 is an example of a simple Bean.
Listing 1-2 Example of a Simple JavaBean
public class SimpleBean implements java.io.Serializable { private String name; // public no-parameter constructor public SimpleBean() { } // getter method for name property public String getName() { return name; } // setter method for name property public void setName(String aName) { name = aName; } }
In this example, String is the type of property and name is the property. Methods that access or set a property are public (available to anyone using the Bean) and also use a certain naming convention. You name these methods as follows:
To get a property’s value, the method must begin with get followed by the property name with the first letter capitalized, as in public String getName();.These methods are called getters.
To set a property’s value, the method must begin with set followed by the property name with the first letter capitalized and the value to set the property to, as in public void setName(String theName);. These methods are called setters.