JavaBeans
Encyclopedia
JavaBeans are reusable
Code reuse
Code reuse, also called software reuse, is the use of existing software, or software knowledge, to build new software.-Overview:Ad hoc code reuse has been practiced from the earliest days of programming. Programmers have always reused sections of code, templates, functions, and procedures...

 software components
Component-based software engineering
Component-based software engineering is a branch of software engineering that emphasizes the separation of concerns in respect of the wide-ranging functionality available throughout a given software system...

 for Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

. Practically, they are classes written in the Java programming language
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

 conforming to a particular convention. They are used to encapsulate many objects into a single object (the bean), so that they can be passed around as a single bean object instead of as multiple individual objects. A JavaBean is a Java Object that is serializable
Serialization
In computer science, in the context of data storage and transmission, serialization is the process of converting a data structure or object state into a format that can be stored and "resurrected" later in the same or another computer environment...

, has a nullary constructor
Nullary constructor
In computer programming, a nullary constructor is a constructor that takes no arguments.-Object-oriented constructors:In object-oriented programming, a constructor is code that is run when an object is created...

, and allows access to properties using getter and setter methods
Mutator method
In computer science, a mutator method is a method used to control changes to a variable.The mutator method, sometimes called a "setter", is most often used in object-oriented programming, in keeping with the principle of encapsulation...

.

Advantages of JavaBeans

  • A Bean obtains all of the benefits of Java's "write once, run anywhere" paradigm.
  • The properties, events, and methods of a Bean that are exposed to another application can be controlled.
  • Auxiliary software can be provided to help configure a Bean.
  • The configuration settings of a Bean can be saved in a persistent storage and can be restored at a later time.
  • A Bean may register to receive events from other objects and can generate events that are sent to other objects.

JavaBeans API

The JavaBeans functionality is provided by a set of classes and interfaces in the java.beans package.
Interface Description
AppletInitializer Methods in this interface are used to initialize Beans that are also applets.
BeanInfo This interface allows the designer to specify information about the events,methods and properties of a Bean.
Customizer This interface allows the designer to provide a graphical user interface through which a bean may be configured.
DesignMode Methods in this interface determine if a bean is executing in design mode.
ExceptionListener A method in this interface is invoked when an exception has occurred.
PropertyChangeListener A method in this interface is invoked when a bound property is changed.
PropertyEditor Objects that implement this interface allow the designer to change and display property values.
VetoableChangeListener A method in this interface is invoked when a Constrained property is changed.
Visibility Methods in this interface allow a bean to execute in environments where GUI is not available.

JavaBean conventions

In order to function as a JavaBean class
Class (computer science)
In object-oriented programming, a class is a construct that is used as a blueprint to create instances of itself – referred to as class instances, class objects, instance objects or simply objects. A class defines constituent members which enable these class instances to have state and behavior...

, an object class must obey certain conventions about method naming, construction, and behaviour. These conventions make it possible to have tools that can use, reuse, replace, and connect JavaBeans.

The required conventions are as follows:
  • The class must have a public default constructor
    Default constructor
    In computer programming languages the term “default constructor” refers to a constructor that is automatically generated in the absence of explicit constructors ; this automatically provided constructor is usually a nullary constructor...

     (no-argument). This allows easy instantiation within editing and activation frameworks.
  • The class properties
    Property (programming)
    A property, in some object-oriented programming languages, is a special sort of class member, intermediate between a field and a method. Properties are read and written like fields, but property reads and writes are translated to get and set method calls...

     must be accessible using get, set, is (used for boolean properties instead of get) and other methods (so-called accessor methods and mutator method
    Mutator method
    In computer science, a mutator method is a method used to control changes to a variable.The mutator method, sometimes called a "setter", is most often used in object-oriented programming, in keeping with the principle of encapsulation...

    s), following a standard naming convention
    Naming conventions (programming)
    In computer programming, a naming convention is a set of rules for choosing the character sequence to be used for identifiers which denote variables, types and functions etc...

    . This allows easy automated inspection and updating of bean state within frameworks, many of which include custom editors for various types of properties. Setters must receive only one argument.
  • The class should be serializable. It allows applications and frameworks to reliably save, store, and restore the bean's state in a fashion independent of the VM
    Virtual machine
    A virtual machine is a "completely isolated guest operating system installation within a normal host operating system". Modern virtual machines are implemented with either software emulation or hardware virtualization or both together.-VM Definitions:A virtual machine is a software...

     and of the platform.

JavaBean Example

PersonBean.java:


package beans;

/**
* Class PersonBean.
*/
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) readable/writable.
*/
public String getName {
return this.name;
}

/**
* Setter for property name.
* @param name
*/
public void setName(final String name) {
this.name = name;
}

/**
* Getter for property "deceased"
* Different syntax for a boolean field (is vs. get)
*/
public boolean isDeceased {
return this.deceased;
}

/**
* Setter for property deceased.
* @param deceased
*/
public void setDeceased(final boolean deceased) {
this.deceased = deceased;
}
}


TestPersonBean.java:


import beans.PersonBean;

/**
* Class TestPersonBean.
*/
public class TestPersonBean {
/**
* Tester method main for class PersonBean.
* @param args
*/
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]");
}
}


testPersonBean.jsp;

<% // Use of PersonBean in a JSP. %>





Name:

Deceased?




Enter a name:

Choose an option:






See also

  • Microsoft COM
    Component Object Model
    Component Object Model is a binary-interface standard for software componentry introduced by Microsoft in 1993. It is used to enable interprocess communication and dynamic object creation in a large range of programming languages...

    , a component implementation on Microsoft Windows
    Microsoft Windows
    Microsoft Windows is a series of operating systems produced by Microsoft.Microsoft introduced an operating environment named Windows on November 20, 1985 as an add-on to MS-DOS in response to the growing interest in graphical user interfaces . Microsoft Windows came to dominate the world's personal...

    .

External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK