A Java Bean is a Java class that follows specific conventions known as Java Bean specifications. When a Java class adheres to these conventions, it is termed a Java Bean. Essentially, a Java Bean is a reusable software component designed to encapsulate data. In this post, we will delve into the characteristics, specifications, advantages, and disadvantages of Java Beans.
A Java Bean is a Java class that should adhere to the following guidelines:
- A default construction should be provided for Java Bean classes. Default constructor is a constructor of a class which has no arguments.
- The java properties of a bean class should all be private. It is not accessible from outside the class. There will be no public properties declared for a java bean.
- Each private property should have a getter and setter method for assigning and reading values. These are the getter and setter methods of a java bean class.
- The java bean class should be serializable. The java bean can be transferred from one device to another via the network.
- The java bean class must be a public class.
Sample Example of a Java Bean
The provided Employee
class exemplifies a Java Bean. It incorporates a default constructor that is not customized, and all of its properties are declared as private variables. Each variable is equipped with public getter and setter methods, ensuring accessibility. Additionally, the class implements the Serializable
interface, facilitating network transfer within the context of a Java Bean.
Employee.java
package com.test; import java.io.Serializable; public class Employee implements Serializable { private int empId; private String name; private double salary; public int getEmpId() { return empId; } public void setEmpId(int empId) { this.empId = empId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } }
How to use a Java Bean
Indeed, a Java Bean is characterized by its inclusion of getter and setter methods. To set values, one employs the setter methods, while getter methods are utilized to retrieve values from the Java Bean. The presence of a default constructor in the Java Bean enables dynamic instantiation without encountering errors. Furthermore, Java variables within the bean are typically designated as private, thereby preventing external modification of these variables from outside the Java Bean object.
package com.test; public class JavaTest { public static void main(String[] args) { Employee employee = new Employee(); employee.setEmpId(100); employee.setName("TestName"); employee.setSalary(1000.00); System.out.println(employee.getEmpId()); System.out.println(employee.getName()); System.out.println(employee.getSalary()); } }
Output
100 TestName 1000.0
Types of Java Bean
Typically, Java Beans are not explicitly categorized as read-write, read-only, or write-only. Instead, they are generally designed to encapsulate data with private fields and provide public getter and setter methods for accessing and modifying that data. Let me clarify:
- Read-Write Java Bean:
- Contains private fields with public getter and setter methods.
- Allows both reading and writing operations on its data.
- Read-Only Java Bean:
- Contains private fields with only getter methods.
- Permits reading of data but does not allow modification.
- Write-Only Java Bean:
- Contains private fields with only setter methods.
- Permits writing data but does not allow reading.
These distinctions are not typically explicitly classified as types of Java Beans. Rather, Java Beans can be designed to allow various levels of access to their encapsulated data based on the provided methods.
ReadOnlyEmployee.java
package com.test; public interface ReadOnlyEmployee { public int getEmpId() ; public String getName() ; public double getSalary() ; }
WriteOnlyEmployee.java
package com.test; public interface WriteOnlyEmployee { public void setEmpId(int empId) ; public void setName(String name) ; public void setSalary(double salary) ; }
Employee.java
package com.test; import java.io.Serializable; public class Employee implements Serializable, ReadOnlyEmployee, WriteOnlyEmployee { private int empId; private String name; private double salary; public int getEmpId() { return empId; } public void setEmpId(int empId) { this.empId = empId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } }
How to use ReadOnly Java Bean
The provided example demonstrates the creation of a read-only Java Bean, designed to facilitate data retrieval without allowing modifications. In this Java Bean, setter methods are encapsulated, making them inaccessible from external sources. Conversely, getter methods are exposed, enabling external usage to retrieve data from the Java Bean. The design ensures that the stored data can be accessed at any time but prohibits alterations, promoting a read-only access pattern for the encapsulated information.
JavaTest.java
package com.test; public class JavaTest { public static void main(String[] args) { ReadOnlyEmployee employee = getReadOnlyEmployee(); System.out.println(employee.getEmpId()); System.out.println(employee.getName()); System.out.println(employee.getSalary()); } public static ReadOnlyEmployee getReadOnlyEmployee() { Employee employee = new Employee(); employee.setEmpId(100); employee.setName("TestName"); employee.setSalary(1000.00); return employee; } }
How to use WriteOnly Java Bean
In the provided example, a write-only Java Bean is illustrated. This type of Java Bean allows the writing of data into it, while restricting access to read operations. The Java Bean exposes setter methods for external use, facilitating the writing of data. However, getter methods are not accessible from outside the Java Bean, preventing the retrieval of stored data. This design ensures that the Java Bean serves exclusively for data writing purposes, maintaining the privacy of its internal state.
JavaTest.java
package com.test; public class JavaTest { public static void main(String[] args) { WriteOnlyEmployee employee = getWriteOnlyEmployee(); employee.setEmpId(100); employee.setName("TestName"); employee.setSalary(1000.00); } public static WriteOnlyEmployee getWriteOnlyEmployee() { Employee employee = new Employee(); employee.setEmpId(100); employee.setName("TestName"); employee.setSalary(1000.00); return employee; } }