by BehindJava

What is a Java bean

Home » java » What is a Java bean

In this tutorial we are going to learn about Java bean in detail, which is an important question in a Java interview.

What is a Java bean?

A JavaBean is a Java class that should follow the following conventions i.e. It should have a no-arg constructor.

It should be Serializable and should provide methods to set and get the values of the properties, known as getter and setter methods.

Why Java bean is required?

Java bean is a reusable software component. A bean encapsulates many objects into one object. So that we can access this object from multiple places across the application. Moreover, it provides easy maintenance.

Java bean is a fully encapsulated class.

Let’s say we have an employee management application which is developed used Spring framework. within the application we have multiple modules related to employee details add, remove, update and fetch.
In this case Java bean class will be useful in getting and setting the values to DAO layer. since all the fields related to employee details in all the four operations i.e. add, remove, update and fetch are same.

Sample Code Snippet:

public class AccessDetails {

	private String gid;
	private String associateName;
	private String status;

	public String getGid() {
		return gid;
	}
	public void setGid(String gid) {
		this.gid = gid;
	}
	public String getAssociateName() {
		return associateName;
	}
	public void setAssociateName(String associateName) {
		this.associateName = associateName;
	}
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}
	
}