Introduction to JavaBeans and its components

This tutorial explains the basics of JavaBeans, its components and how JavaBeans are helpful.

Introduction

One of the most important topics in software component model is reusability. Writing self-contained software under the moto of “Developed them once, run and reuse them everywhere” is most required and appreciated. So reusability is added into the Java programming language with the help of JavaBeans.

What are JavaBeans?

JavaBeans are introduced in 1996 by Sun Microsystem and defined as

“A JavaBean is reusable, platform independent component that can be manipulated visually in a builder tool.”

In computing, based on the Java Platform, JavaBeans are classes that encapsulate many objects into a single object (the bean). Builder tool enables you to create and use beans for application development purpose. In simple words JavaBean is nothing but a Java class. When these JavaBeans are used in other applications, the internal working of such components are hidden from the application developer.

Example:

All Swing and AWT classes are JavaBeans. GUI components are ideal JavaBeans.

JavaBean

JavaBean

Components of JavaBeans

The classes that contained definition of beans is known as components of JavaBeans. These classes follows certain design conventions. It includes properties, events, methods and persistence. There are two types of components, GUI based and non GUI based. For instance JButton is example of a component not a class.




Properties (date members): Property is a named attribute of a bean, it includes color, label, font, font size, display size. It determines appearance, behavior and state of a bean.

Methods: Methods in JavaBeans are same as normal Java methods in a class. It doesn’t follow any specific naming conventions. All properties should have accessor and getter methods.

Events: Events in JavaBeans are same as SWING/AWT event handling.

Persistence: Serializable interface enables JavaBean to store its state.

JavaBean has no argument constructor.

JavaBean component model

JavaBean component

JavaBeans Properties

JavaBean property can be access by the user of the object, it can be read, write, read only or write only. We can access these JavaBeans properties with the help of getPropertyName() method also known as getter or accessor and setPropertyName() method known as setter written in implementation class of bean.

GetPropertyName(): For example, if property name is Title, your method name would be geTitle().

SetPropertyName(): For example, if property name  is Title, your method name would be setTitle().

Example of JavaBeans

Before going to write a JavaBean, here are some basic rules. A JavaBean should be public, should has no argument default constructor and should implement serializable interface. Keep these basic rules in mind before writing a JavaBean.

Here is a simple example of JavaBean.

public class BankAccount implements java.io.Serializable {

	
		   private String accountNumber = null;
		   private String totalAmount = null; 
		   private String accountHolderName = null;
		   private int accountHolderAge = 0;
		   private String accountHolderAddress = null;
		   
		public String getAccountNumber() {
			return accountNumber;
		}
		public void setAccountNumber(String accountNumber) {
			this.accountNumber = accountNumber;
		}
		public String getTotalAmount() {
			return totalAmount;
		}
		public void setTotalAmount(String totalAmount) {
			this.totalAmount = totalAmount;
		}
		public String getAccountHolderName() {
			return accountHolderName;
		}
		public void setAccountHolderName(String accountHolderName) {
			this.accountHolderName = accountHolderName;
		}
		public int getAccountHolderAge() {
			return accountHolderAge;
		}
		public void setAccountHolderAge(int accountHolderAge) {
			this.accountHolderAge = accountHolderAge;
		}
		public String getAccountHolderAddress() {
			return accountHolderAddress;
		}
		public void setAccountHolderAddress(String accountHolderAddress) {
			this.accountHolderAddress = accountHolderAddress;
		}	  
		
}

Advantages of JavaBeans

Following are some advantages of JavaBeans:

  • Reusability in different environments.
  • Used to create applet, servlet, application or other components.
  • JavaBeans are dynamic, can be customized.
  • Can be deployed in network systems

Here is the link of source code, you can download it from here

Share on FacebookTweet about this on TwitterShare on LinkedInShare on Google+Digg thisPin on PinterestShare on RedditShare on StumbleUponPrint this pageEmail this to someone

Leave a Reply

Be the First to Comment!

avatar
wpDiscuz