Free Data Structures and Algorithms Course

Subscribe below and get all best seller courses for free !!!


Subscribe to all free courses

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.
© 2021 Learn Java by Examples Template by Hubberspot