HOME

TheInfoList



OR:

In computing based on the
Java Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's List ...
Platform, JavaBeans is a technology developed by
Sun Microsystems Sun Microsystems, Inc. (Sun for short) was an American technology company that sold computers, computer components, software, and information technology services and created the Java programming language, the Solaris operating system, ZFS, the ...
and released in 1996, as part of
JDK The Java Development Kit (JDK) is a distribution of Java Technology by Oracle Corporation. It implements the Java Language Specification (JLS) and the Java Virtual Machine Specification (JVMS) and provides the Standard Edition (SE) of the Java ...
1.1. The 'beans' of JavaBeans are classes that encapsulate one or more
objects Object may refer to: General meanings * Object (philosophy), a thing, being, or concept ** Object (abstract), an object which does not exist at any particular time or place ** Physical object, an identifiable collection of matter * Goal, an ...
into a single standardized object (the bean). This standardization allows the beans to be handled in a more generic fashion, allowing easier
code reuse In software development (and computer programming in general), code reuse, also called software reuse, is the use of existing software, or software knowledge, to build new software, following the reusability principles. Code reuse may be achiev ...
and
introspection Introspection is the examination of one's own conscious thoughts and feelings. In psychology, the process of introspection relies on the observation of one's mental state, while in a spiritual context it may refer to the examination of one's s ...
. This in turn allows the beans to be treated as
software components Component-based software engineering (CBSE), also called component-based development (CBD), is a branch of software engineering that emphasizes the separation of concerns with respect to the wide-ranging functionality available throughout a give ...
, and to be manipulated visually by editors and IDEs without needing any initial configuration, or to know any internal implementation details. As part of the standardization, all beans must be serializable, have a zero-argument constructor, and allow access to properties using getter and setter methods.


Features

;Introspection :Introspection is a process of analyzing a Bean to determine its capabilities. This is an essential feature of the Java Beans specification because it allows another application, such as a design tool, to obtain information about a component. ;Properties :A property is a subset of a Bean's state. The values assigned to the properties determine the behaviour and appearance of that component. They are set through a setter method and can be obtained by a getter method. ;Customization :A customizer can provide a step-by-step guide that the process must follow to use the component in a specific context. ;Events :Beans may interact with the EventObject EventListener model. ;Persistence :Persistence is the ability to save the current state of a Bean, including the values of a Bean's properties and instance variables, to nonvolatile storage and to retrieve them at a later time. ;Methods :A Bean should use accessor methods to encapsulate the properties. A Bean can provide other methods for business logic not related to the access to the properties.


Advantages

* The properties, events, and methods of a bean can be exposed to another application. * A bean may register to receive events from other objects and can generate events that are sent to those other objects. * Auxiliary software can be provided to help configure a bean. *The configuration settings of a bean can be saved to persistent storage and restored.


Disadvantages

* A class with a zero-argument constructor is subject to being instantiated in an invalid state. If such a class is instantiated manually by a developer (rather than automatically by some kind of framework), the developer might not realize that the class has been improperly instantiated. The compiler cannot detect such a problem, and even if it is documented, there is no guarantee that the developer will see the documentation. * JavaBeans are inherently mutable and so lack the advantages offered by immutable objects. * Having to create getters for every property and setters for many, most, or all of them can lead to an immense quantity of
boilerplate code In computer programming, boilerplate code, or simply boilerplate, are sections of code that are repeated in multiple places with little to no variation. When using languages that are considered ''verbose'', the programmer must write a lot of boile ...
.


JavaBeans API

The JavaBeans functionality is provided by a set of classes and interfaces in the java.beans package.


JavaBean conventions

In order to function as a JavaBean
class Class or The Class may refer to: Common uses not otherwise categorized * Class (biology), a taxonomic rank * Class (knowledge representation), a collection of individuals or objects * Class (philosophy), an analytical concept used differentl ...
, 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 Java Beans. The required conventions are as follows: * The class must have a public
default constructor In computer programming languages, the term default constructor can refer to a constructor that is automatically generated by the compiler in the absence of any programmer-defined constructors (e.g. in Java), and is usually a nullary constructor. I ...
(with no arguments). This allows easy instantiation within editing and activation frameworks. * The class
properties Property is the ownership of land, resources, improvements or other tangible objects, or intellectual property. Property may also refer to: Mathematics * Property (mathematics) Philosophy and science * Property (philosophy), in philosophy and ...
must be accessible using ''get'', ''set'', ''is'' (can be used for boolean properties instead of get), ''to'' and other methods (so-called accessor methods and
mutator method In computer science, a mutator method is a method used to control changes to a variable. They are also widely known as setter methods. Often a setter is accompanied by a getter (together also known as accessors), which returns the value of the priva ...
s) according to a standard
naming convention A naming convention is a convention (generally agreed scheme) for naming things. Conventions differ in their intents, which may include to: * Allow useful information to be deduced from the names based on regularities. For instance, in Manhatta ...
. This allows easy automated inspection and updating of bean state within frameworks, many of which include custom editors for various types of properties. Setters can have one or more arguments. * The class should be serializable. (This allows applications and frameworks to reliably save, store, and restore the bean's state in a manner independent of the VM and of the platform.)


Code example

package player; public class PersonBean implements java.io.Serializable TestPersonBean.java: import player.PersonBean; /** * Class "TestPersonBean". */ public class TestPersonBean Name:
Deceased?

Enter a name:
Choose an option:


References

{{Reflist, 2


External links


Oracle's JavaBeans tutorials


Java platform Articles with example Java code Architectural pattern (computer science) Software design patterns