Pitch your project

Random text






Blogs

0    
By admin Category Uncategorized, Posted March 20th, 2016
JavaBeans

JavaBeans refer to Java classes which encapsulate many objects into a single object known as the bean. The beans are serializable meaning that you can not only store the bean but also you can store the beans state. Java beans have a nullary constructor. A nullary constructor is a constructor with no arguments.Consider the following example of a nullary Constructor.

public class Person {
/*nullary constructor*/
public Person()
{
this(0);
}
/*not nullary constructor*/
public Person(int age) {
this.age=age;
}
protected int age;
}

JavaBean conventions

Conventions are like rules that a class should comply with in order to be a JavaBean. They include:
* The class must have a nullary constructor
* The bean properties must be accessed using the get and set methods
* The bean must be serializable

JavaBeans allow access to properties using getter and setter methods. Some people may ask what was the whole intention of coming up with the JavaBeans while we have other data structures which can also contain many objects in themselves for example we can have a List of Objects. Here are some of the few cons and pros of using java beans.

Advantages

  • The properties, events and methods of a java bean can be controlled that is on how they are exposed to the other applications and also how they can be accessed by the other applications and also what they can do in the applications that can access them.
  • A bean can have events being listened by other applications and it can also have listeners listening on other applications.
  • Other software can be used to setup a JavaBean
  • The state of a JavaBeans can be saved to a persistent storage and restored back to the state it was in back in the ram.

Disadvantages

  • Since a java beans class constructor is nullary it is highly susceptible to be instantiated with an error especially when its done manually.
  • Since every property of a bean should have its own getters and setters the code can be quit bulky.

JavaBeans API

The JavaBeans API is contained in the java.beans package.This package contains classes and interfaces which explain the functionality of the JavaBeans API.

JavaBeans API Interfaces

1. AppletInitializer
2. BeanInfo
3. Customizer
4. DesignMode
5. ExceptionListener
6. PropertyChangeListener
6. PropertyEditor
7. VetoableChangeListener
8. Visibility.

JavaBeans API Classes Summary

1. BeanDescriptor
2. Beans
3. Encoder
4. EventHandler
5. EventSetDescriptor
6. Expression
7. FeatureDescriptor
8. IndexedPropertyChangeEvent
9. IndexedPropertyDescriptor
10. Introspector
11. MethodDescriptor
12. ParameterDescriptor
13. PersistenceDelegate

JavaBeans Exception Summary

* IntrospectionException
* PropertyVetoException

JavaBeans Example

public class Person implements Serializable {
private String name;
private int age;

 public Person() { }

 public Person(String name, int age)
       {
 this.name=name;
 this.age=age;
     }

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

 public String getName() {
 return name;
        }

 public void setAge(int age) {
 this.age=age;
       }

 public int  getAge() {
 return age;
      }
 }

Posted by James Kae

Leave a Reply

Your email address will not be published. Required fields are marked *