Bean Internals  «Prev  Next»
Lesson 1

JavaBeans Internals

This module takes you inside a Bean to learn about the structure and organization of data and methods, the two primary parts of a Bean. You learn all about properties, which model the public portion of a Bean's data and are accessible externally through accessor methods.
You take a look at the different types of properties, including simple properties, indexed properties, bound properties, and constrained properties.
Toward the end of the module you work through a practical exercise of creating a property and its associated accessor methods.
  • Module Learning Objectives:
    After completing this module, you will have the knowledge and skills necessary to:
    1. Design and code properties for your own Beans
    2. Define accessor methods used to access properties
    3. Understand when and why to use indexed, bound, and constrained properties



Function of 1) simple properties, 2) indexed properties, 3) bound properties, and 4) constrained properties

JavaBeans, a component architecture for Java, uses several types of properties to define the characteristics of a bean. Here's a breakdown of the four types you've mentioned:
  1. Simple Properties
    • Function: These are basic properties where a bean has getter and setter methods for direct access and modification.
    • Example:
                private String name;
      
                public String getName() {
                  return name;
                }
      
                public void setName(String name) {
                  this.name = name;
                }
              
    • Usage: Used for straightforward data access without any additional logic or constraints.
  2. Indexed Properties
    • Function: These properties represent an array or collection where individual elements can be accessed or modified by index.
    • Example:
                private String[] items;
      
                public String[] getItems() {
                  return items;
                }
      
                public void setItems(String[] items) {
                  this.items = items;
                }
      
                public String getItem(int index) {
                  return items[index];
                }
      
                public void setItem(int index, String item) {
                  items[index] = item;
                }
              
    • Usage: Useful when you need to manage collections within a bean, allowing for both bulk and individual element manipulation.
  3. Bound Properties
    • Function: These properties notify listeners when their values change. This is useful for maintaining consistency across multiple beans or updating UI components automatically.
    • Example:
                private String title;
                private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
      
                public void addPropertyChangeListener(PropertyChangeListener listener) {
                  propertyChangeSupport.addPropertyChangeListener(listener);
                }
      
                public void removePropertyChangeListener(PropertyChangeListener listener) {
                  propertyChangeSupport.removePropertyChangeListener(listener);
                }
      
                public String getTitle() {
                  return title;
                }
      
                public void setTitle(String title) {
                  String oldTitle = this.title;
                  this.title = title;
                  propertyChangeSupport.firePropertyChange("title", oldTitle, title);
                }
              
    • Usage: Ideal for scenarios where changes in one bean should trigger updates in other parts of the application.
  4. Constrained Properties
    • Function: These properties allow other objects to veto a change before it happens. This ensures that certain conditions are met before a property can be changed.
    • Example:
                private int age;
                private VetoableChangeSupport vetoableChangeSupport = new VetoableChangeSupport(this);
      
                public void addVetoableChangeListener(VetoableChangeListener
      
      


The two primary components of a JavaBean are indeed:
  1. Data: Represented by private fields that encapsulate the object's state. These fields typically store the bean's properties, which define its characteristics and behavior.
  2. Methods: Public methods that provide access to and manipulate the object's data. These methods allow other parts of your program to interact with the bean, get or set its properties, and trigger its actions.

By following these conventions, JavaBeans become reusable and modular components that can be easily integrated into different applications. This makes them a valuable tool for developers who want to build clean, maintainable, and flexible software.


Developing Java Beans

JavaBeans Architecture

The JavaBeans architecture is supposed to scale well from small to large; simple beans can be used to build larger beans. A small bean may consist of a single class; a large bean may have many. Beans can also work together through their container to provide services to other beans. Simple beans are little more than ordinary Java objects.
  1. In fact, any Java class that has a default (empty) constructor could be considered a bean.
  2. A bean should also be serializable, although the JavaBeans specification does not strictly require that.
These two criteria ensure that we can create an instance of the bean dynamically and that we can later save the bean as part of a group or composition of beans. There are no other requirements. Beans are not required to inherit from a base bean class, and they do not have to implement any special interface. A useful bean should send and receive events and expose its properties to the world. To do so, it follows the appropriate design patterns for naming the relevant methods so that these features can be automatically discovered. Most nontrivial beans intended for use in a visual application builder IDE also provide information about themselves in the form of a BeanInfo class. A BeanInfo class implements the BeanInfo interface, which holds methods that describe a bean's features in more detail, along with extra packaging, such as icons for display to the user. Normally, this "bean info" is supplied by a separate class that is named for and supplied with the bean.
In the next lesson, the structural makeup of a Bean will be discused.

SEMrush Software