Bean Introspection  «Prev  Next»
Lesson 9Building a basic JavaBean
ObjectiveHow do you Build and Test a basic Bean using Properties and Accessor Methods

Build and Test a basic Bean using Properties and Accessor Methods

How do you build and test a Javabean using Properties and Accessor Methods

To build and test a Java Bean with properties and accessor methods, you can follow these steps:
  1. Define the class: a) Define a class with instance variables to hold the properties of the bean.
    b) Make the class implement the Serializable interface.
  2. Add accessor methods:
    Add get and set methods to access and set the properties of the bean. These methods are called accessor methods.
  3. Provide a no-arg constructor:
    Provide a no-arg constructor for the bean, which is required for Java Beans to be used in some contexts.
  4. Compile the Bean:
    Compile the Java Bean into a .class file using the javac command.
  5. Test the Bean:
    Create a simple test program that creates an instance of the bean, sets its properties, and retrieves them to verify that they were set correctly.
Here is an example of a simple Java Bean with a property named name:
import java.io.Serializable;

public class ExampleBean implements Serializable {
    private String name;

    public ExampleBean() {}

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}


And here is an example of a test program:
public class ExampleBeanTest {
    public static void main(String[] args) {
        ExampleBean bean = new ExampleBean();
        bean.setName("John Doe");
        System.out.println(bean.getName()); // Output: "John Doe"
    }
}

Building Custom JavaBean

The MyLabel Bean serves as your first custom Bean created from scratch.
Even in its simplicity, the development of this Bean demonstrates a great deal of what is involved in building any Bean. The code for the Bean also serves to combine many of the concepts you've learned throughout the course thus far, not to mention putting them in a practical context. I encourage you to really spend some time working with this Bean.
Feel free to explore making changes in the source code and seeing how they impact the Bean's behavior. Keep in mind that it takes hands-on practice to get proficient with any programming endeavor, and JavaBeans is no different. So, please take the time to tinker with the MyLabel Bean until you are completely comfortable with the code and the resulting behavior of the Bean.


Building a basic JavaBean

Up to this point in the course you've learned a lot about the composition of Beans and how Beans are organized. This module closes on a practical note by allowing you to build a basic Bean based on a lot of the things you have learned thus far. In this lesson, you build a Bean that displays a text label.
The basic Bean you build in this lesson, MyLabel, contains a single property called label that is displayed as the Bean's only graphical output.
This Bean can be used as an output control to display text. The MyLabel Bean consists of the following major parts:
  1. A label property
  2. Constructors for the Bean
  3. Accessor methods for the label property
  4. A paint() method to draw the label text

Basic Bean - Exercise

Click the Exercise link below to build the MyLabel Bean.
The next lesson concludes this module. In it, you review what you covered up to this point followed by a short quiz.
Basic Bean Exercise