Introduction to JavaBean

Introduction

This article explains Java Beans. The NetBeans IDE is used for the sample examples.

What is JavaBean

A Java Bean is simply a Java class that is mainly responsible for holding onto some data without a large degree of functionality built into the class.

A Java class is said to a Bean class if they have the following properties:

  • has no-arguments constructors.
  • implements the Serializable interface.
  • Exposes its properties via getter/setter methods.

JavaBeans Properties:

Its property may be read, write, read-only, or write-only. The JavaBean properties are accessed through two methods, they are:

  • getPropertyName()

          Used to get the the property; for example if we have a property named uname then it should be getUname.

  • setPropertyName()

          Used to set the property; for example if we have a property named uname then it should be setUname.

Advantages

The following are the Advantages:

  • It may be possible to register to receive events from other objects and can generate an event that sent to other objects.
  • It provides the ability for properties, events, and methods of a bean to be reused in another application.
  • The configuration setting of a bean can be saved in a persistent storage and restored at a later time.

Disadvantages

The following are the Disadvantages:

  • A class with a nullary 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) then the developer might not realize that the class has been improperly instantiated. The compiler can't detect such a problem, and even if it's documented, there's no guarantee that the developer will see the documentation.
  • Having to create a getter for every property and a setter for many, most, or all of them can lead to an immense quantity of boilerplate code.

Syntax

public class ClassName implements java.io.Serializable
{
/**
* Property <code>name</code> (note capitalization) readable/writable.
*/
//Define property-name
//Getter/Setter method
}

Example

In this example we create a Bean class. This class contains a user id, name and email id. This properties of the Bean class is used in the "DisplayUserProperty" class in which the values of various parameters are passed and displayed on the console. Let's have a look.

Create the following files in the NetBeans IDE.

1. Users.java

public class Users implements java.io.Serializable {

 

    private int id;

    private String name, emailid;

 

    public int getId() {

        return id;

    }

 

    public void setId(int id) {

        this.id = id;

    }

 

    public String getName() {

        return name;

    }

 

    public void setName(String name) {

        this.name = name;

    }

 

    public String getEmailid() {

        return emailid;

    }

 

    public void setEmailid(String emailid) {

        this.emailid = emailid;

    }

 

}

2. DisplayUsersProperty.java

public class DisplayUserProperties {

 

    public static void main(String args[]) {

        Users u = new Users();

        u.setId(20);

        u.setEmailid("[email protected]");

        u.setName("Ram");

        System.out.println("Name of user is: " + u.getName());

        System.out.println("ID of user is: " + u.getId());

        System.out.println("Email-ID of user is: " + u.getEmailid());

 

    }

}

Now run the "DisplayUserProperties.java" file. The following output is generated:

Output

Up Next
    Ebook Download
    View all
    Learn
    View all