Introduction to JavaBean

Introduction to Java Beans

This article explains basics of Java Beans. JavaBeans is a set of Java Bean classes. 

What is a JavaBean?

 
A Java Bean is a Java class that is mainly responsible for holding onto some data without a large degree of functionality built into the class.
A Java class is said to a Bean class if they have the following properties:
  • Has no-arguments constructors.
  • Implements the Serializable interface.
  • Exposes its properties via getter/setter methods.

JavaBeans Properties

  • Its property may be read, write, read-only, or write-only. The JavaBean properties are accessed through two methods, they are: getPropertyName()
  • Used to get the the property; for example if we have a property named uname then it should be getUname: setPropertyName()
  • Used to set the property; for example if we have a property named uname then
  •  it should be setUname.

Advantages of JavaBeans

 
The following are the advantages of using JavaBeans:
  • It may be possible to register to receive events from other objects and can generate an event that sent to other objects.
  • It provides the ability for properties, events, and methods of a bean to be reused in another application.
  • The configuration setting of a bean can be saved in a persistent storage and restored at a later time.

Disadvantages of JavaBeans

 
The following are the disadvantages of JavaBeans: 
  • A class with a nullary constructor is subject to being instantiated in an invalid state. If such a class is instantiated manually by a developer (rather than automatically by some kind of framework) then the developer might not realize that the class has been improperly instantiated. The compiler can't detect such a problem, and even if it's documented, there's no guarantee that the developer will see the documentation. 
  • Having to create a getter for every property and a setter for many, most, or all of them can lead to an immense quantity of boilerplate code. 

Java Bean Class Definition

 
Here is a Java Bean class definition
  1. public class ClassName implements java.io.Serializable
  2. {
  3. /**
  4. * Property <code>name</code> (note capitalization) readable/writable.
  5. */
  6. //Define property-name
  7. //Getter/Setter method
  8. }

Java Bean Code Example

 
In this example, we create a Bean class. This class contains a user id, name and email id. This properties of the Bean class is used in the "DisplayUserProperty" class in which the values of various parameters are passed and displayed on the console.
 
Let's have a look.
 
Create the following files in the NetBeans IDE - Users.java and DisplayUsersProperty.java.
 
1. Users.java 
  1. public class Users implements java.io.Serializable {  
  2. private int id;  
  3. private String name, emailid;  
  4. public int getId() {  
  5. return id;  
  6. }  
  7. public void setId(int id) {  
  8. this.id = id;  
  9. }  
  10. public String getName() {  
  11. return name;  
  12. }  
  13. public void setName(String name) {  
  14. this.name = name;  
  15. }  
  16. public String getEmailid() {  
  17. return emailid;  
  18. }  
  19. public void setEmailid(String emailid) {  
  20. this.emailid = emailid;  
  21. }  
  22. }
2. DisplayUsersProperty.java 
  1. public class DisplayUserProperties {  
  2. public static void main(String args[]) {  
  3. Users u = new Users();  
  4. u.setId(20);  
  5. u.setEmailid("[email protected]");  
  6. u.setName("Ram");  
  7. System.out.println("Name of user is: " + u.getName());  
  8. System.out.println("ID of user is: " + u.getId());  
  9. System.out.println("Email-ID of user is: " + u.getEmailid());  
  10. }  
  11. }  
Now run the "DisplayUserProperties.java" file.
 
The following output is generated:
 
Java Bean  
 


Similar Articles