What is Java Bean?

JavaBean is a software component model for developing reusable software components in Java. It is a simple and powerful way to create software components that we can easily integrate into other applications.

A JavaBean is a simple Java class that has the following conventions:

  • It should have private variables
  • It should have a no-argument constructor.
  • It should be Serializable to make it transferable in the Network.
  • It should provide methods to set and get the values of the properties
    • We can call these methods a getter and setter.
    • The setter method’s return type should be void

Properties

  • A JavaBean property is a named attribute that we can access by the user of the object. 
  • The attribute can be of any Java data type, including the classes that we define.
  • A JavaBean property can be read, write, read-only, or write-only.
  • We can access JavaBean properties through two methods in JavaBean’s implementation class. Example:
MethodDescriptions
getPropertyName()For example, if the property name is firstName, your method name would be getFirstName() to read that property. This method is called an accessor.
setPropertyName()
For example, if the property name is firstName, your method name would be setFirstName() to write that property. This method is called mutator.
  • A read-only attribute will have only a getPropertyName() method, and a write-only attribute will have only a setPropertyName() method.

JavaBean class Example

public class Student {
	private int rollNo;
	private String name;
	private char gender;

	public int getRollNo() {
		return rollNo;
	}

	public void setRollNo(int rollNo) {
		this.rollNo = rollNo;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public char getGender() {
		return gender;
	}

	public void setGender(char gender) {
		this.gender = gender;
	}
}
Sharing Is Caring:
Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments