Pin It

How to create a simple JavaBean component ?.

JavaBeans are the powerful reusable components created for reducing redundant code . The JavaBean has few rules associated with it . The JavaBean creation process is easy and simple . The process of creating a JavaBean is demonstrated below .

The Bean creating process :

In order to create a bean , we have to define a java class . The class must have a no-argument constructor , should have a accessor and mutator methods and should be serializable by implementing an interface called "Serializable" . Accessor methods are also called as getters and Mutators methods are called as setters. The property of the JavaBean should be accessed , modified by these setters and getters.

Let us look into an simple example :

package com.hubberspot.javabeans.example;


public class SimpleJavaBean implements Serializable
  protected int value;
  public SimpleJavaBean()
    value = 0;

  public void setValue(int newValue)
    value = newValue;

  public int getValue()
    return value;

In the above example, the bean has one property called value. The accessor method is getValue() and the mutator method is setValue().

Compiling the Bean

The bean class should be compiled as a regular java file using javac compiler.
Please Give Us Your 1 Minute In Sharing This Post!
Related Posts Plugin for WordPress, Blogger...
© 2012 Learn Java by Examples Template by Hubber Spot