JavaBean Class in Java

JavaBean Class in JAVA

In this tutorial, we will learn about JavaBean Class in Java. A JavaBean serves as a reusable software component in Java, adhering to specific conventions that simplify integration into Java-based applications. Typically, JavaBeans handles the encapsulation and management of data within an application. Moreover, they find frequent use in graphical user interface (GUI) development. In this context, components such as buttons, text fields, and checkboxes are commonly represented as JavaBeans.

JavaBean Characteristics

  • Properties: JavaBeans has instance variables that are exposed using getter and setter methods. These variables are often referred to as properties. For example, a JavaBean representing a person might have properties like “name,” “age,” and “address.”
  • Public Default Constructor: JavaBeans has a public no-argument constructor, also known as a default constructor. This is necessary because JavaBeans can be instantiated using the Class.forName() method, which relies on the presence of a default constructor.
  • Serializable: JavaBeans often implement the Serializable interface, which allows them to be easily serialized and deserialized. This is important for saving and restoring the state of JavaBeans, such as in JavaEE applications.
  • Events: JavaBeans can generate and listen for events. They can notify other parts of the application when certain actions or changes occur. For example, a button JavaBean might generate an event when it’s clicked.
  • Naming Conventions: JavaBeans follow specific naming conventions for getter and setter methods. For a property named “propertyName,” the getter should be named “getPropertyName” and the setter should be named “setPropertyName.”

Difference Between JavaBean and Java Class

Java ClassJavaBean
General-purpose blueprint for objects.
Reusable component designed for integration.
Naming ConventionsNo strict naming conventions.Follows strict naming conventions for properties, getters, and setters.
SerializableNo specific requirement to implement Serializable.Often implements the Serializable interface for serialization support.
No-Argument ConstructorNo specific requirement for a no-argument constructor.Must have a public no-argument constructor (default constructor).
Special InterfacesNo specific interfaces required.May implement specific interfaces like Serializable.
Access ModifiersFields and methods can have various access modifiers.Typically, fields are private with public getter and setter methods.
EventsNo specific event handling mechanisms.Can generate and listen for events.
Examplejava class MyClass { … }java public class PersonBean { private String name; public String getName() { … } public void setName(String name) { … } }

In summary, both Java classes and JavaBeans serve as foundational components for creating objects in Java. However, JavaBeans represents a specialized subset of classes intentionally crafted with conventions and requirements. These specifications render them exceptionally well-suited for reuse as components across diverse Java-based applications, particularly in contexts such as graphical user interface (GUI) development and JavaEE applications. On the contrary, Java classes maintain a broader, more adaptable nature, devoid of any stringent prerequisites or conventions being enforced upon them.

Properties of JavaBean

JavaBean properties, which represent the attributes or characteristics of a JavaBean object, become accessible and manageable via getter and setter methods. This approach enables you to encapsulate data effectively and offer controlled access. Furthermore, JavaBean properties adhere to specific naming conventions, simplifying their usage in diverse Java-based applications.

  • Naming Conventions: JavaBean properties follow a naming convention where the property name is combined with “get” and “set” prefixes for the getter and setter methods, respectively. For example, if you have a property named “name,” the getter method should be named “getName” and the setter method should be named “setName.”
  • Access Control: It’s common practice to make the fields (instance variables) representing properties private to encapsulate the data and provide controlled access through getter and setter methods. By doing this, you can enforce rules or validations when getting or setting property values.
  • Data Type: Properties can have various data types, such as strings, numbers, booleans, or custom objects, depending on the requirements of the JavaBean.
  • Getter Method: The getter method is used to retrieve the value of a property. It typically has the form public DataType getPropertyName(), where DataType is the data type of the property.
  • Setter Method: The setter method is used to set the value of a property. It typically has the form public void setPropertyName(DataType value), where DataType is the data type of the property.

Difference between JavaBeans and POJO

JavaBeansPOJO (Plain Old Java Object)
PurposeDesigned for creating reusable components in various Java-based applications, often used in GUI development and JavaEE.General-purpose Java classes used for various purposes, including data transfer objects (DTOs), model objects, and more.
Naming ConventionsFollows strict naming conventions for properties, getter and setter methods. For example, “getName” and “setName” for a property “name.”No strict naming conventions; fields and methods can be named as needed.
Access ControlOften uses private fields with getter and setter methods to encapsulate data and provide controlled access.Can have fields with various access modifiers (public, private, protected, etc.), offering more flexibility in access control.
Default ConstructorTypically includes a public no-argument constructor (default constructor) due to potential instantiation using reflection.While common, a default constructor is not a strict requirement.
SerializableOften implements the Serializable interface to support serialization and deserialization.Implementing Serializable is optional and depends on specific use cases and requirements.

In summary, JavaBeans are specialized Java classes with strict naming conventions, often implementing Serializable, and typically having a default constructor. They are designed for creating reusable components. In contrast, POJOs are general-purpose Java classes without strict conventions, providing more flexibility in access control and usage for various purposes. The choice between JavaBeans and POJOs depends on the specific needs of your application.

JavaBean Example


public class PersonBean implements Serializable {
    private String name;
    private int age;

    // Default constructor (no-argument constructor)
    public PersonBean() {
        // Default constructor is required for JavaBeans

    // Getter method for the 'name' property
    public String getName() {
        return name;

    // Setter method for the 'name' property
    public void setName(String name) { = name;

    // Getter method for the 'age' property
    public int getAge() {
        return age;

    // Setter method for the 'age' property
    public void setAge(int age) {
        this.age = age;

    public static void main(String[] args) {
        // Create an instance of PersonBean
        PersonBean person = new PersonBean();
        // Set values using setter methods
        // Retrieve values using getter methods
        String personName = person.getName();
        int personAge = person.getAge();
        // Display the information
        System.out.println("Name: " + personName);
        System.out.println("Age: " + personAge);


Name: Deepak
Age: 30

Discover Our Exciting Courses and Quiz

Enroll now to enhance your skills and knowledge!

Java Online Quiz

Level up your coding skills with our interactive programming quiz!