BookRags.com Literature Guides Literature
Guides
Criticism & Essays Criticism &
Essays
Questions & Answers Questions &
Answers
Lesson Plans Lesson
Plans
My Bibliography Periodic Table U.S. Presidents Shakespeare Sonnet Shake-Up
Research Anything:        
History | Encyclopedias | Films | News | Create a Bibliography | More... Login | Register | Help

JavaBean

Print-Friendly
About 2 pages (463 words)

Bookmark and Share Know this topic well? Help others and get FREE products!

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".

Contents

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

<source lang="java"> // PersonBean.java public class PersonBean implements java.io.Serializable {

   private String name;
   private boolean deceased;
   // No-arg constructor (takes no arguments).
   public PersonBean() {
   }
   public String getName() {
       return this.name;
   }
   public void setName(String name) {
       this.name = name;
   }
   // Different semantics for a boolean field (is vs. get)
   public boolean isDeceased() {
       return this.deceased;
   }
   public void setDeceased(boolean deceased) {
       this.deceased = deceased;
   }

} </source> <source lang="java"> // 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]");
   }

} </source>

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.

See also

External links

View More Summaries on JavaBean
 
Ask any question on JavaBean and get it answered FAST!
Answer questions in BookRags Q&A and earn points toward
discounted or even FREE Study Guides and other BookRags products!
Learn more about BookRags Q&A
Copyrights
JavaBean from Wíkipedia. ©2006 by Wíkipedia. Licensed under the GNU Free Documentation License. View a list of authors or edit this article.

Article Navigation
Join BookRagslearn moreJoin BookRags




About BookRags | Customer Service | Report an Error | Terms of Use | Privacy Policy