Jump to content

JavaBeans: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
No edit summary
Line 68: Line 68:


== Trivia ==
== Trivia ==
'Java beans' also refers to [Coffea arabica].
''Java beans'' also refers to [[Coffea arabica]].


== See also ==
== See also ==

Revision as of 11:24, 5 March 2008

JavaBeans are classes written in the Java programming language conforming to a particular convention. They are used to encapsulate many objects into a single object (the bean), so that the bean can be passed around rather than the individual objects.

The specification by Sun Microsystems defines them as "reusable software components that can be manipulated visually in a builder tool".

JavaBean conventions

In order to function as a JavaBean class, an object class must obey certain conventions about method naming, construction, and behavior. These conventions make it possible to have tools that can use, reuse, replace, and connect JavaBeans.

The required conventions are:

  • The class must have a no-argument public constructor. This allows easy instantiation by editing and activation frameworks.
  • Its properties must be accessible using get, set and other methods (so called accessor methods) following a standard naming convention. This allows easy automated inspection and updating of bean state by frameworks, many of which include custom editors for various types of properties.
  • The class should be serializable. This allows applications and frameworks to reliably save, store and restore bean state in a VM and platform independent fashion.
  • It should not contain any required event-handling methods.

Because these requirements are largely expressed as conventions rather than by implementing interfaces, some developers view Java Beans as Plain Old Java Objects that follow certain naming conventions.

JavaBean Example

// PersonBean.java

public class PersonBean implements java.io.Serializable {
    
    private String name;
    private boolean deceased;

    // No-arg constructor (takes no arguments).
    public PersonBean() {
    }

    // Property "name" (note capitalization) readadble/writable
    public String getName() {
        return this.name;
    }
    public void setName(String name) {
        this.name = name;
    }

    // Property "deceased"
    // Different syntax for a boolean field (is vs. get)
    public boolean isDeceased() {
        return this.deceased;
    }
    public void setDeceased(boolean deceased) {
        this.deceased = deceased;
    }
}
// TestPersonBean.java

public class TestPersonBean {
    public static void main(String[] args) {

        PersonBean person = new PersonBean();
        person.setName("Bob");
        person.setDeceased(false);

        // Output: "Bob [alive]"
        System.out.print(person.getName());
        System.out.println(person.isDeceased() ? " [deceased]" : " [alive]");
    }
}

Adoption

AWT, Swing, and SWT, the major Java GUI toolkits, use JavaBeans conventions for their components. This allows GUI editors like the Eclipse Visual Editor or the NetBeans GUI Editor to maintain a hierarchy of components and to provide access to their properties via uniformly-named accessors and mutators.

Trivia

Java beans also refers to Coffea arabica.

See also